Skip to content

Commit 2a39f34

Browse files
committed
SPR-8803 Refine UriComponentsBuilder.replaceQueryParam().
If no values are given, the query parameter is removed.
1 parent dc88a7c commit 2a39f34

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
404404

405405
/**
406406
* Sets the query parameter values overriding all existing query values for the same parameter.
407-
* If no values are given, the resulting URI will contain the query parameter name only.
407+
* If no values are given, the query parameter is removed.
408408
*
409409
* @param name the query parameter name
410410
* @param values the query parameter values
@@ -413,7 +413,9 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
413413
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
414414
Assert.notNull(name, "'name' must not be null");
415415
this.queryParams.remove(name);
416-
queryParam(name, values);
416+
if (!ObjectUtils.isEmpty(values)) {
417+
queryParam(name, values);
418+
}
417419
return this;
418420
}
419421

org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void replaceQueryParam() {
240240
builder.replaceQueryParam("baz");
241241
result = builder.build();
242242

243-
assertEquals("baz", result.getQuery());
243+
assertNull("Query param should have been deleted", result.getQuery());
244244
}
245245

246246
}

spring-framework-reference/src/mvc.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,11 +2905,14 @@ URI uri = uriComponents.expand("42", "21").encode().toUri();
29052905
<para>In a Servlet environment the
29062906
<classname>ServletUriComponentsBuilder</classname> sub-class provides
29072907
static factory methods to copy available URL information from a
2908-
Servlet request including host, scheme, port, path and query string:
2908+
Servlet requests:
29092909
</para>
29102910

29112911
<programlisting language="java">HttpServletRequest request = ...
29122912

2913+
// Re-use host, scheme, port, path and query string
2914+
// Replace the "accountId" query param
2915+
29132916
ServletUriComponentsBuilder ucb =
29142917
ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build()
29152918
.expand("123")
@@ -2919,7 +2922,9 @@ ServletUriComponentsBuilder ucb =
29192922
<para>Alternatively, you may choose to copy a subset of the available
29202923
information up to and including the context path:</para>
29212924

2922-
<programlisting language="java">// Host, port and context path
2925+
<programlisting language="java">// Re-use host, port and context path
2926+
// Append "/accounts" to the path
2927+
29232928
ServletUriComponentsBuilder ucb =
29242929
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
29252930
</programlisting>
@@ -2928,7 +2933,10 @@ ServletUriComponentsBuilder ucb =
29282933
by name (e.g. <literal>/main/*</literal>), you can also have the literal part
29292934
of the servlet mapping included:</para>
29302935

2931-
<programlisting language="java">// Host, port, context path, and the literal part of the servlet mapping
2936+
<programlisting language="java">// Re-use host, port, context path
2937+
// Append the literal part of the servlet mapping to the path
2938+
// Append "/accounts" to the path
2939+
29322940
ServletUriComponentsBuilder ucb =
29332941
ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()
29342942
</programlisting>

0 commit comments

Comments
 (0)