diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
index 3b168d3f75..7a307290cc 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
@@ -404,7 +404,7 @@ public class UriComponentsBuilder {
/**
* Sets the query parameter values overriding all existing query values for the same parameter.
- * If no values are given, the resulting URI will contain the query parameter name only.
+ * If no values are given, the query parameter is removed.
*
* @param name the query parameter name
* @param values the query parameter values
@@ -413,7 +413,9 @@ public class UriComponentsBuilder {
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
Assert.notNull(name, "'name' must not be null");
this.queryParams.remove(name);
- queryParam(name, values);
+ if (!ObjectUtils.isEmpty(values)) {
+ queryParam(name, values);
+ }
return this;
}
diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java
index 2dfc28bec1..0890729c23 100644
--- a/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java
+++ b/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java
@@ -240,7 +240,7 @@ public class UriComponentsBuilderTests {
builder.replaceQueryParam("baz");
result = builder.build();
- assertEquals("baz", result.getQuery());
+ assertNull("Query param should have been deleted", result.getQuery());
}
}
diff --git a/spring-framework-reference/src/mvc.xml b/spring-framework-reference/src/mvc.xml
index 332394ea6e..1ddf737e17 100644
--- a/spring-framework-reference/src/mvc.xml
+++ b/spring-framework-reference/src/mvc.xml
@@ -2905,11 +2905,14 @@ URI uri = uriComponents.expand("42", "21").encode().toUri();
In a Servlet environment the
ServletUriComponentsBuilder sub-class provides
static factory methods to copy available URL information from a
- Servlet request including host, scheme, port, path and query string:
+ Servlet requests:
HttpServletRequest request = ...
+// Re-use host, scheme, port, path and query string
+// Replace the "accountId" query param
+
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build()
.expand("123")
@@ -2919,7 +2922,9 @@ ServletUriComponentsBuilder ucb =
Alternatively, you may choose to copy a subset of the available
information up to and including the context path:
-// Host, port and context path
+// Re-use host, port and context path
+// Append "/accounts" to the path
+
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
@@ -2928,7 +2933,10 @@ ServletUriComponentsBuilder ucb =
by name (e.g. /main/*), you can also have the literal part
of the servlet mapping included:
-// Host, port, context path, and the literal part of the servlet mapping
+// Re-use host, port, context path
+// Append the literal part of the servlet mapping to the path
+// Append "/accounts" to the path
+
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()