SPR-8803 Refine UriComponentsBuilder.replaceQueryParam().

If no values are given, the query parameter is removed.
This commit is contained in:
Rossen Stoyanchev 2011-11-10 15:08:24 +00:00
parent dc88a7c8ba
commit 2a39f34d33
3 changed files with 16 additions and 6 deletions

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -2905,11 +2905,14 @@ URI uri = uriComponents.expand("42", "21").encode().toUri();
<para>In a Servlet environment the
<classname>ServletUriComponentsBuilder</classname> 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:
</para>
<programlisting language="java">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 =
<para>Alternatively, you may choose to copy a subset of the available
information up to and including the context path:</para>
<programlisting language="java">// Host, port and context path
<programlisting language="java">// Re-use host, port and context path
// Append "/accounts" to the path
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
</programlisting>
@ -2928,7 +2933,10 @@ ServletUriComponentsBuilder ucb =
by name (e.g. <literal>/main/*</literal>), you can also have the literal part
of the servlet mapping included:</para>
<programlisting language="java">// Host, port, context path, and the literal part of the servlet mapping
<programlisting language="java">// 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()
</programlisting>