SPR-8803 Refine UriComponentsBuilder.replaceQueryParam().
If no values are given, the query parameter is removed.
This commit is contained in:
parent
dc88a7c8ba
commit
2a39f34d33
|
@ -404,7 +404,7 @@ public class UriComponentsBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the query parameter values overriding all existing query values for the same parameter.
|
* 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 name the query parameter name
|
||||||
* @param values the query parameter values
|
* @param values the query parameter values
|
||||||
|
@ -413,7 +413,9 @@ public class UriComponentsBuilder {
|
||||||
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
|
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
|
||||||
Assert.notNull(name, "'name' must not be null");
|
Assert.notNull(name, "'name' must not be null");
|
||||||
this.queryParams.remove(name);
|
this.queryParams.remove(name);
|
||||||
queryParam(name, values);
|
if (!ObjectUtils.isEmpty(values)) {
|
||||||
|
queryParam(name, values);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -240,7 +240,7 @@ public class UriComponentsBuilderTests {
|
||||||
builder.replaceQueryParam("baz");
|
builder.replaceQueryParam("baz");
|
||||||
result = builder.build();
|
result = builder.build();
|
||||||
|
|
||||||
assertEquals("baz", result.getQuery());
|
assertNull("Query param should have been deleted", result.getQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2905,11 +2905,14 @@ URI uri = uriComponents.expand("42", "21").encode().toUri();
|
||||||
<para>In a Servlet environment the
|
<para>In a Servlet environment the
|
||||||
<classname>ServletUriComponentsBuilder</classname> sub-class provides
|
<classname>ServletUriComponentsBuilder</classname> sub-class provides
|
||||||
static factory methods to copy available URL information from a
|
static factory methods to copy available URL information from a
|
||||||
Servlet request including host, scheme, port, path and query string:
|
Servlet requests:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<programlisting language="java">HttpServletRequest request = ...
|
<programlisting language="java">HttpServletRequest request = ...
|
||||||
|
|
||||||
|
// Re-use host, scheme, port, path and query string
|
||||||
|
// Replace the "accountId" query param
|
||||||
|
|
||||||
ServletUriComponentsBuilder ucb =
|
ServletUriComponentsBuilder ucb =
|
||||||
ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build()
|
ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build()
|
||||||
.expand("123")
|
.expand("123")
|
||||||
|
@ -2919,7 +2922,9 @@ ServletUriComponentsBuilder ucb =
|
||||||
<para>Alternatively, you may choose to copy a subset of the available
|
<para>Alternatively, you may choose to copy a subset of the available
|
||||||
information up to and including the context path:</para>
|
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 ucb =
|
||||||
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
|
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
@ -2928,7 +2933,10 @@ ServletUriComponentsBuilder ucb =
|
||||||
by name (e.g. <literal>/main/*</literal>), you can also have the literal part
|
by name (e.g. <literal>/main/*</literal>), you can also have the literal part
|
||||||
of the servlet mapping included:</para>
|
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 ucb =
|
||||||
ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()
|
ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
Loading…
Reference in New Issue