Skip to content

Commit 0d3fb0e

Browse files
committed
Add Kotlin code snippets to WebMvc refdoc
See gh-21778
1 parent 7cfae94 commit 0d3fb0e

File tree

4 files changed

+2098
-558
lines changed

4 files changed

+2098
-558
lines changed

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

Lines changed: 142 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
`UriComponentsBuilder` helps to build URI's from URI templates with variables, as the following example shows:
66

7-
[source,java,indent=0]
8-
[subs="verbatim,quotes"]
7+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
8+
.Java
99
----
1010
UriComponents uriComponents = UriComponentsBuilder
1111
.fromUriString("https://example.com/hotels/{hotel}") // <1>
@@ -21,12 +21,28 @@
2121
<4> Build a `UriComponents`.
2222
<5> Expand variables and obtain the `URI`.
2323

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`.
2440

2541
The preceding example can be consolidated into one chain and shortened with `buildAndExpand`,
2642
as the following example shows:
2743

28-
[source,java,indent=0]
29-
[subs="verbatim,quotes"]
44+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
45+
.Java
3046
----
3147
URI uri = UriComponentsBuilder
3248
.fromUriString("https://example.com/hotels/{hotel}")
@@ -35,28 +51,54 @@ as the following example shows:
3551
.buildAndExpand("Westin", "123")
3652
.toUri();
3753
----
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+
----
3864

3965
You can shorten it further by going directly to a URI (which implies encoding),
4066
as the following example shows:
4167

42-
[source,java,indent=0]
43-
[subs="verbatim,quotes"]
68+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
69+
.Java
4470
----
4571
URI uri = UriComponentsBuilder
4672
.fromUriString("https://example.com/hotels/{hotel}")
4773
.queryParam("q", "{q}")
4874
.build("Westin", "123");
4975
----
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+
----
5084

5185
You shorter it further still with a full URI template, as the following example shows:
5286

53-
[source,java,indent=0]
54-
[subs="verbatim,quotes"]
87+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
88+
.Java
5589
----
5690
URI uri = UriComponentsBuilder
5791
.fromUriString("https://example.com/hotels/{hotel}?q={q}")
5892
.build("Westin", "123");
5993
----
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+
60102

61103

62104

@@ -76,39 +118,62 @@ exposes shared configuration options.
76118

77119
The following example shows how to configure a `RestTemplate`:
78120

79-
[source,java,indent=0]
80-
[subs="verbatim,quotes"]
121+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
122+
.Java
81123
----
82124
// import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode;
83125
84126
String baseUrl = "https://example.org";
85127
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
86-
factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VARIABLES);
128+
factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VALUES);
87129
88130
RestTemplate restTemplate = new RestTemplate();
89131
restTemplate.setUriTemplateHandler(factory);
90132
----
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+
----
91145

92146
The following example configures a `WebClient`:
93147

94-
[source,java,indent=0]
95-
[subs="verbatim,quotes"]
148+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
149+
.Java
96150
----
97151
// import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode;
98152
99153
String baseUrl = "https://example.org";
100154
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
101-
factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VARIABLES);
155+
factory.setEncodingMode(EncodingMode.TEMPLATE_AND_VALUES);
102156
103157
WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
104158
----
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+
----
105170

106171
In addition, you can also use `DefaultUriBuilderFactory` directly. It is similar to using
107172
`UriComponentsBuilder` but, instead of static factory methods, it is an actual instance
108173
that holds configuration and preferences, as the following example shows:
109174

110-
[source,java,indent=0]
111-
[subs="verbatim,quotes"]
175+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
176+
.Java
112177
----
113178
String baseUrl = "https://example.com";
114179
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);
@@ -117,6 +182,16 @@ that holds configuration and preferences, as the following example shows:
117182
.queryParam("q", "{q}")
118183
.build("Westin", "123");
119184
----
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+
----
120195

121196

122197

@@ -144,44 +219,68 @@ URI variables intentionally contain reserved characters.
144219

145220
The following example uses the first option:
146221

147-
[source,java,indent=0]
148-
[subs="verbatim,quotes"]
222+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
223+
.Java
149224
----
150-
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
225+
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
151226
.queryParam("q", "{q}")
152227
.encode()
153228
.buildAndExpand("New York", "foo+bar")
154229
.toUri();
155230
156231
// Result is "/hotel%20list/New%20York?q=foo%2Bbar"
157232
----
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+
----
158244

159245
You can shorten the preceding example by going directly to the URI (which implies encoding),
160246
as the following example shows:
161247

162-
[source,java,indent=0]
163-
[subs="verbatim,quotes"]
248+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
249+
.Java
164250
----
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}")
166259
.queryParam("q", "{q}")
167260
.build("New York", "foo+bar")
168261
----
169262

170263
You can shorten it further still with a full URI template, as the following example shows:
171264

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")
174270
----
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}")
176275
.build("New York", "foo+bar")
177276
----
178277

179278
The `WebClient` and the `RestTemplate` expand and encode URI templates internally through
180279
the `UriBuilderFactory` strategy. Both can be configured with a custom strategy.
181280
as the following example shows:
182281

183-
[source,java,indent=0]
184-
[subs="verbatim,quotes"]
282+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
283+
.Java
185284
----
186285
String baseUrl = "https://example.com";
187286
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl)
@@ -194,6 +293,22 @@ as the following example shows:
194293
// Customize the WebClient..
195294
WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
196295
----
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+
----
197312

198313
The `DefaultUriBuilderFactory` implementation uses `UriComponentsBuilder` internally to
199314
expand and encode URI templates. As a factory, it provides a single place to configure

0 commit comments

Comments
 (0)