@@ -104,29 +104,24 @@ An example of using the `DefaultUriBuilderFactory`:
104
104
= URI Encoding
105
105
[.small]#Spring MVC and Spring WebFlux#
106
106
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:
108
110
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.
111
115
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.
115
118
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.
122
122
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:
130
125
131
126
[source,java,indent=0]
132
127
[subs="verbatim,quotes"]
@@ -135,11 +130,13 @@ shown below:
135
130
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl)
136
131
factory.setEncodingMode(EncodingMode.VALUES_ONLY);
137
132
138
- // ...
133
+ WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
134
+
135
+ RestTemplate restTemplate = new RestTemplate();
136
+ restTemplate.setUriTemplateHandler(factory);
139
137
----
140
138
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.
145
142
0 commit comments