Skip to content

Commit 4ec9f5d

Browse files
committed
Minor polishing for URI encoding docs
1 parent 224fcc1 commit 4ec9f5d

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/docs/asciidoc/web/web-uris.adoc

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,24 @@ An example of using the `DefaultUriBuilderFactory`:
104104
= URI Encoding
105105
[.small]#Spring MVC and Spring WebFlux#
106106

107-
The default way of encoding URIs in `UriComponents` works as follows:
107+
By default `UriComponents` encodes only characters that are illegal within a given URI
108+
component, but not all characters with reserved meaning. More specifically `UriComponents`
109+
does the following:
108110

109-
. URI variables are expanded.
110-
. Each URI component (path, query, etc) is encoded individually.
111+
. Expand URI variables.
112+
. Encode each URI component (path, query, etc) individually, by applying percent encoding
113+
to illegal characters such as non-US-ASCII characters as well as any characters that are
114+
illegal within the URI component, as per RFC 3986.
111115

112-
The rules for encoding are as follows: within a URI component, apply percent encoding to
113-
all illegal characters, including non-US-ASCII characters, and all other characters that
114-
are illegal within the URI component, as defined in RFC 3986.
116+
This is comparable to the way the `java.net.URI` multi-argument constructor works and is
117+
described in the "Escaped octets, quotation, encoding, and decoding" section of its Javadoc.
115118

116-
[TIP]
117-
====
118-
The encoding in `UriComponents` is comparable to the multi-argument constructor of
119-
`java.net.URI`, as described in the "Escaped octets, quotation, encoding, and decoding"
120-
section of its class-level Javadoc.
121-
====
119+
In some cases, you may want to ensure that expanded URI variables do not impact the
120+
structure and meaning of the URI. That means encoding not only illegal characters but also
121+
all characters with reserved meaning in a URI.
122122

123-
The above default encoding strategy *does not* encode all characters with reserved
124-
meaning, but only the ones that are illegal within a given URI component. If this is not
125-
what you expect, you can use the alternative strategy described next.
126-
127-
When using <<web-uribuilder,DefaultUriBuilderFactory>> -- plugged into the `WebClient`,
128-
`RestTemplate`, or used directly, you can switch to an alternative encoding strategy as
129-
shown below:
123+
The `WebClient` and the `RestTemplate` can be switched to a different encoding mode
124+
through the <<web-uribuilder,UriBuilderFactory>> strategy:
130125

131126
[source,java,indent=0]
132127
[subs="verbatim,quotes"]
@@ -135,11 +130,13 @@ shown below:
135130
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl)
136131
factory.setEncodingMode(EncodingMode.VALUES_ONLY);
137132
138-
// ...
133+
WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
134+
135+
RestTemplate restTemplate = new RestTemplate();
136+
restTemplate.setUriTemplateHandler(factory);
139137
----
140138

141-
This alternative encoding strategy applies `UriUtils.encode(String, Charset)` to each URI
142-
variable value prior to expanding it, effectively encoding all non-US-ASCII characters,
143-
and *all* characters with reserved meaning in a URI, which ensures that the expanded URI
144-
variables do not have any impact on the structure or meaning of the URI.
139+
Internally `DefaultUriBuilderFactory` delegates to `UriUtils.encode(String, Charset)` to
140+
encode each URI variable value prior to expanding it, effectively encoding both all
141+
non-US-ASCII characters, and characters with reserved meaning in a URI.
145142

0 commit comments

Comments
 (0)