4
4
5
5
`UriComponentsBuilder` helps to build URI's from URI templates with variables, as the following example shows:
6
6
7
- [source,java,indent=0]
8
- [subs="verbatim,quotes"]
7
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
8
+ .Java
9
9
----
10
10
UriComponents uriComponents = UriComponentsBuilder
11
11
.fromUriString("https://example.com/hotels/{hotel}") // <1>
21
21
<4> Build a `UriComponents`.
22
22
<5> Expand variables and obtain the `URI`.
23
23
24
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
25
+ .Kotlin
26
+ ----
27
+ val uriComponents = UriComponentsBuilder
28
+ .fromUriString("https://example.com/hotels/{hotel}") // <1>
29
+ .queryParam("q", "{q}") // <2>
30
+ .encode() // <3>
31
+ .build() // <4>
32
+
33
+ val uri = uriComponents.expand("Westin", "123").toUri() // <5>
34
+ ----
35
+ <1> Static factory method with a URI template.
36
+ <2> Add or replace URI components.
37
+ <3> Request to have the URI template and URI variables encoded.
38
+ <4> Build a `UriComponents`.
39
+ <5> Expand variables and obtain the `URI`.
24
40
25
41
The preceding example can be consolidated into one chain and shortened with `buildAndExpand`,
26
42
as the following example shows:
27
43
28
- [source,java,indent=0]
29
- [subs="verbatim,quotes"]
44
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
45
+ .Java
30
46
----
31
47
URI uri = UriComponentsBuilder
32
48
.fromUriString("https://example.com/hotels/{hotel}")
@@ -35,28 +51,54 @@ as the following example shows:
35
51
.buildAndExpand("Westin", "123")
36
52
.toUri();
37
53
----
54
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
55
+ .Kotlin
56
+ ----
57
+ val uri = UriComponentsBuilder
58
+ .fromUriString("https://example.com/hotels/{hotel}")
59
+ .queryParam("q", "{q}")
60
+ .encode()
61
+ .buildAndExpand("Westin", "123")
62
+ .toUri()
63
+ ----
38
64
39
65
You can shorten it further by going directly to a URI (which implies encoding),
40
66
as the following example shows:
41
67
42
- [source,java,indent=0]
43
- [subs="verbatim,quotes"]
68
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
69
+ .Java
44
70
----
45
71
URI uri = UriComponentsBuilder
46
72
.fromUriString("https://example.com/hotels/{hotel}")
47
73
.queryParam("q", "{q}")
48
74
.build("Westin", "123");
49
75
----
76
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
77
+ .Kotlin
78
+ ----
79
+ val uri = UriComponentsBuilder
80
+ .fromUriString("https://example.com/hotels/{hotel}")
81
+ .queryParam("q", "{q}")
82
+ .build("Westin", "123")
83
+ ----
50
84
51
85
You shorter it further still with a full URI template, as the following example shows:
52
86
53
- [source,java,indent=0]
54
- [subs="verbatim,quotes"]
87
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
88
+ .Java
55
89
----
56
90
URI uri = UriComponentsBuilder
57
91
.fromUriString("https://example.com/hotels/{hotel}?q={q}")
58
92
.build("Westin", "123");
59
93
----
94
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
95
+ .Kotlin
96
+ ----
97
+ val uri = UriComponentsBuilder
98
+ .fromUriString("https://example.com/hotels/{hotel}?q={q}")
99
+ .build("Westin", "123")
100
+ ----
101
+
60
102
61
103
62
104
@@ -76,39 +118,62 @@ exposes shared configuration options.
76
118
77
119
The following example shows how to configure a `RestTemplate`:
78
120
79
- [source,java,indent=0]
80
- [subs="verbatim,quotes"]
121
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
122
+ .Java
81
123
----
82
124
// import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode;
83
125
84
126
String baseUrl = "https://example.org";
85
127
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
86
- factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VARIABLES );
128
+ factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VALUES );
87
129
88
130
RestTemplate restTemplate = new RestTemplate();
89
131
restTemplate.setUriTemplateHandler(factory);
90
132
----
133
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
134
+ .Kotlin
135
+ ----
136
+ // import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode
137
+
138
+ val baseUrl = "https://example.org"
139
+ val factory = DefaultUriBuilderFactory(baseUrl)
140
+ factory.encodingMode = EncodingMode.TEMPLATE_AND_VALUES
141
+
142
+ val restTemplate = RestTemplate()
143
+ restTemplate.uriTemplateHandler = factory
144
+ ----
91
145
92
146
The following example configures a `WebClient`:
93
147
94
- [source,java,indent=0]
95
- [subs="verbatim,quotes"]
148
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
149
+ .Java
96
150
----
97
151
// import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode;
98
152
99
153
String baseUrl = "https://example.org";
100
154
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
101
- factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VARIABLES );
155
+ factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VALUES );
102
156
103
157
WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
104
158
----
159
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
160
+ .Kotlin
161
+ ----
162
+ // import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode
163
+
164
+ val baseUrl = "https://example.org"
165
+ val factory = DefaultUriBuilderFactory(baseUrl)
166
+ factory.encodingMode = EncodingMode.TEMPLATE_AND_VALUES
167
+
168
+ val client = WebClient.builder().uriBuilderFactory(factory).build()
169
+ ----
105
170
106
171
In addition, you can also use `DefaultUriBuilderFactory` directly. It is similar to using
107
172
`UriComponentsBuilder` but, instead of static factory methods, it is an actual instance
108
173
that holds configuration and preferences, as the following example shows:
109
174
110
- [source,java,indent=0]
111
- [subs="verbatim,quotes"]
175
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
176
+ .Java
112
177
----
113
178
String baseUrl = "https://example.com";
114
179
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);
@@ -117,6 +182,16 @@ that holds configuration and preferences, as the following example shows:
117
182
.queryParam("q", "{q}")
118
183
.build("Westin", "123");
119
184
----
185
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
186
+ .Kotlin
187
+ ----
188
+ val baseUrl = "https://example.com"
189
+ val uriBuilderFactory = DefaultUriBuilderFactory(baseUrl)
190
+
191
+ val uri = uriBuilderFactory.uriString("/hotels/{hotel}")
192
+ .queryParam("q", "{q}")
193
+ .build("Westin", "123")
194
+ ----
120
195
121
196
122
197
@@ -144,44 +219,68 @@ URI variables intentionally contain reserved characters.
144
219
145
220
The following example uses the first option:
146
221
147
- [source,java,indent=0]
148
- [subs="verbatim,quotes"]
222
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
223
+ .Java
149
224
----
150
- URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
225
+ URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
151
226
.queryParam("q", "{q}")
152
227
.encode()
153
228
.buildAndExpand("New York", "foo+bar")
154
229
.toUri();
155
230
156
231
// Result is "/hotel%20list/New%20York?q=foo%2Bbar"
157
232
----
233
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
234
+ .Kotlin
235
+ ----
236
+ val uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
237
+ .queryParam("q", "{q}")
238
+ .encode()
239
+ .buildAndExpand("New York", "foo+bar")
240
+ .toUri()
241
+
242
+ // Result is "/hotel%20list/New%20York?q=foo%2Bbar"
243
+ ----
158
244
159
245
You can shorten the preceding example by going directly to the URI (which implies encoding),
160
246
as the following example shows:
161
247
162
- [source,java,indent=0]
163
- [subs="verbatim,quotes"]
248
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
249
+ .Java
164
250
----
165
- URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
251
+ URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
252
+ .queryParam("q", "{q}")
253
+ .build("New York", "foo+bar")
254
+ ----
255
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
256
+ .Kotlin
257
+ ----
258
+ val uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
166
259
.queryParam("q", "{q}")
167
260
.build("New York", "foo+bar")
168
261
----
169
262
170
263
You can shorten it further still with a full URI template, as the following example shows:
171
264
172
- [source,java,indent=0]
173
- [subs="verbatim,quotes"]
265
+ [source,java,indent=0,subs="verbatim,quotes",role="primary"]
266
+ .Java
267
+ ----
268
+ RI uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
269
+ .build("New York", "foo+bar")
174
270
----
175
- URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
271
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
272
+ .Kotlin
273
+ ----
274
+ val uri = UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}")
176
275
.build("New York", "foo+bar")
177
276
----
178
277
179
278
The `WebClient` and the `RestTemplate` expand and encode URI templates internally through
180
279
the `UriBuilderFactory` strategy. Both can be configured with a custom strategy.
181
280
as the following example shows:
182
281
183
- [source,java,indent=0]
184
- [subs="verbatim,quotes"]
282
+ [source,java,indent=0,subs="verbatim,quotes",role="primary" ]
283
+ .Java
185
284
----
186
285
String baseUrl = "https://example.com";
187
286
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl)
@@ -194,6 +293,22 @@ as the following example shows:
194
293
// Customize the WebClient..
195
294
WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
196
295
----
296
+ [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
297
+ .Kotlin
298
+ ----
299
+ val baseUrl = "https://example.com"
300
+ val factory = DefaultUriBuilderFactory(baseUrl).apply {
301
+ encodingMode = EncodingMode.TEMPLATE_AND_VALUES
302
+ }
303
+
304
+ // Customize the RestTemplate..
305
+ val restTemplate = RestTemplate().apply {
306
+ uriTemplateHandler = factory
307
+ }
308
+
309
+ // Customize the WebClient..
310
+ val client = WebClient.builder().uriBuilderFactory(factory).build()
311
+ ----
197
312
198
313
The `DefaultUriBuilderFactory` implementation uses `UriComponentsBuilder` internally to
199
314
expand and encode URI templates. As a factory, it provides a single place to configure
0 commit comments