Minor fixes: UriComponentsBuilder, UriComponents, docs

After the latest changes, two small fixes in the clone method to copy
the encode flag, and in the encodeUriTemplate method to account for
possible null query params.

Improvements in the URI encoding section.

Issue: SPR-17039, SPR-17027
This commit is contained in:
Rossen Stoyanchev
2018-07-19 09:11:57 -04:00
parent 1cd0135195
commit 34a0cdfc33
4 changed files with 81 additions and 33 deletions

View File

@@ -8,9 +8,8 @@
[source,java,indent=0]
[subs="verbatim,quotes"]
----
String uriTemplate = "http://example.com/hotels/{hotel}";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(uriTemplate) // <1>
UriComponents uriComponents = UriComponentsBuilder
.fromUriString("http://example.com/hotels/{hotel}") // <1>
.queryParam("q", "{q}") // <2>
.encode() // <3>
.build(); // <4>
@@ -23,18 +22,40 @@
<4> Build a `UriComponents`.
<5> Expand variables, and obtain the `URI`.
The above can also be done in shorthand form:
The above can be consolidated into one chain and shortened with `buildAndExpand`:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
URI uri = UriComponentsBuilder
.fromUriString("http://example.com/hotels/{hotel}")
.queryParam("q", "{q}")
.encode()
.buildAndExpand("Westin", "123")
.toUri();
----
It can be shortened further by going directly to URI (which implies encoding):
[source,java,indent=0]
[subs="verbatim,quotes"]
----
URI uri = UriComponentsBuilder
.fromUriString("http://example.com/hotels/{hotel}")
.queryParam("q", "{q}")
.build("Westin", "123");
----
Or shorter further yet, with a full URI template:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
URI uri = UriComponentsBuilder
.fromUriString("http://example.com/hotels/{hotel}?q={q}")
.build("Westin", "123");
----
[[web-uribuilder]]
= UriBuilder
@@ -125,15 +146,34 @@ Example usage using option 1:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
UriComponentsBuilder.fromPath("/hotel list/{city}")
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
.queryParam("q", "{q}")
.encode()
.buildAndexpand("New York", "foo+bar")
.toUriString();
.buildAndExpand("New York", "foo+bar")
.toUri();
// Result is "/hotel%20list/New%20York?foo%2Bbar"
----
The above can be shortened by going directly to URI (which implies encoding):
[source,java,indent=0]
[subs="verbatim,quotes"]
----
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
.queryParam("q", "{q}")
.build("New York", "foo+bar")
----
Or shorter further yet, with a full URI template:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
.build("New York", "foo+bar")
----
The `WebClient` and the `RestTemplate` expand and encode URI templates internally through
the `UriBuilderFactory` strategy. Both can be configured with a custom strategy: