You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/integration/rest-clients.adoc
+37-33
Original file line number
Diff line number
Diff line change
@@ -13,12 +13,12 @@ The Spring Framework provides the following choices for making calls to REST end
13
13
== `RestClient`
14
14
15
15
The `RestClient` is a synchronous HTTP client that offers a modern, fluent API.
16
-
It offers an abstraction over HTTP libraries that allows for convenient conversion from Java object to HTTP request, and creation of objects from the HTTP response.
16
+
It offers an abstraction over HTTP libraries that allows for convenient conversion from a Java object to an HTTP request, and the creation of objects from an HTTP response.
17
17
18
18
=== Creating a `RestClient`
19
19
20
20
The `RestClient` is created using one of the static `create` methods.
21
-
You can also use `builder` to get a builder with further options, such as specifying which HTTP library to use (see <<rest-request-factories>>) and which message converters to use (see <<rest-message-conversion>>), setting a default URI, default path variables, a default request headers, or `uriBuilderFactory`, or registering interceptors and initializers.
21
+
You can also use `builder()` to get a builder with further options, such as specifying which HTTP library to use (see <<rest-request-factories>>) and which message converters to use (see <<rest-message-conversion>>), setting a default URI, default path variables, default request headers, or `uriBuilderFactory`, or registering interceptors and initializers.
22
22
23
23
Once created (or built), the `RestClient` can be used safely by multiple threads.
24
24
@@ -64,35 +64,36 @@ val customClient = RestClient.builder()
64
64
=== Using the `RestClient`
65
65
66
66
When making an HTTP request with the `RestClient`, the first thing to specify is which HTTP method to use.
67
-
This can be done with `method(HttpMethod)`, or with the convenience methods `get()`, `head()`, `post()`, and so on.
67
+
This can be done with `method(HttpMethod)` or with the convenience methods `get()`, `head()`, `post()`, and so on.
68
68
69
69
==== Request URL
70
70
71
71
Next, the request URI can be specified with the `uri` methods.
72
-
This step is optional, and can be skipped if the `RestClient` is configured with a default URI.
73
-
The URL is typically specified as `String`, with optional URI template variables.
72
+
This step is optional and can be skipped if the `RestClient` is configured with a default URI.
73
+
The URL is typically specified as a `String`, with optional URI template variables.
74
74
String URLs are encoded by default, but this can be changed by building a client with a custom `uriBuilderFactory`.
75
75
76
-
The URL can also be provided with a function, or as `java.net.URI`, both of which are not encoded.
76
+
The URL can also be provided with a function or as a `java.net.URI`, both of which are not encoded.
77
77
For more details on working with and encoding URIs, see xref:web/webmvc/mvc-uri-building.adoc[URI Links].
78
78
79
79
==== Request headers and body
80
80
81
-
If necessary, the HTTP request can be manipulated, by adding request headers with `header(String, String)`, `headers(Consumer<HttpHeaders>`, or with the convenience methods `accept(MediaType...)`, `acceptCharset(Charset...)` and so on.
82
-
For HTTP request that can contain a body (`POST`, `PUT`, and `PATCH`), additional methods are available: `contentType(MediaType)`, and `contentLength(long)`.
81
+
If necessary, the HTTP request can be manipulated by adding request headers with `header(String, String)`, `headers(Consumer<HttpHeaders>`, or with the convenience methods `accept(MediaType...)`, `acceptCharset(Charset...)` and so on.
82
+
For HTTP requests that can contain a body (`POST`, `PUT`, and `PATCH`), additional methods are available: `contentType(MediaType)`, and `contentLength(long)`.
83
83
84
84
The request body itself can be set by `body(Object)`, which internally uses <<rest-message-conversion>>.
85
85
Alternatively, the request body can be set using a `ParameterizedTypeReference`, allowing you to use generics.
86
86
Finally, the body can be set to a callback function that writes to an `OutputStream`.
87
87
88
88
==== Retrieving the response
89
+
89
90
Once the request has been set up, the HTTP response is accessed by invoking `retrieve()`.
90
-
The response body can be accessed by using `body(Class)`, or `body(ParameterizedTypeReference)` for parameterized types like lists.
91
-
The `body` method converts the response contents into various types, for instance bytes can be converted into a `String`, JSON into objects using Jackson, and so on (see <<rest-message-conversion>>).
91
+
The response body can be accessed by using `body(Class)` or `body(ParameterizedTypeReference)` for parameterized types like lists.
92
+
The `body` method converts the response contents into various types – for instance, bytes can be converted into a `String`, JSON can be converted into objects using Jackson, and so on (see <<rest-message-conversion>>).
92
93
93
94
The response can also be converted into a `ResponseEntity`, giving access to the response headers as well as the body.
94
95
95
-
This sample shows how `RestClient` can be used to perform a simple GET request.
96
+
This sample shows how `RestClient` can be used to perform a simple `GET` request.
`RestClient` can convert JSON to objects, using the Jackson library.
174
-
Note the usage of uri variables in this sample, and that the `Accept` header is set to JSON.
175
+
Note the usage of URI variables in this sample and that the `Accept` header is set to JSON.
175
176
176
177
[tabs]
177
178
======
@@ -248,8 +249,9 @@ val response = restClient.post() <2>
248
249
======
249
250
250
251
==== Error handling
252
+
251
253
By default, `RestClient` throws a subclass of `RestClientException` when retrieving a response with a 4xx or 5xx status code.
252
-
This behavior can be overriden using `onStatus`.
254
+
This behavior can be overridden using `onStatus`.
253
255
254
256
[tabs]
255
257
======
@@ -286,8 +288,9 @@ val result = restClient.get() <1>
286
288
======
287
289
288
290
==== Exchange
289
-
For more advanced scenarios, the `RestClient` gives access to the underlying HTTP request and response through the `exchange` method, which can be used instead of `retrieve()`.
290
-
Status handlers are not applied when you exchange, because the exchange function already provides access to the full response, allowing you to perform any error handling necessary.
291
+
292
+
For more advanced scenarios, the `RestClient` gives access to the underlying HTTP request and response through the `exchange()` method, which can be used instead of `retrieve()`.
293
+
Status handlers are not applied when use `exchange()`, because the exchange function already provides access to the full response, allowing you to perform any error handling necessary.
291
294
292
295
[tabs]
293
296
======
@@ -336,6 +339,7 @@ val result = restClient.get()
336
339
337
340
[[rest-message-conversion]]
338
341
=== HTTP Message Conversion
342
+
339
343
[.small]#xref:web/webflux/reactive-spring.adoc#webflux-codecs[See equivalent in the Reactive stack]#
340
344
341
345
The `spring-web` module contains the `HttpMessageConverter` interface for reading and writing the body of HTTP requests and responses through `InputStream` and `OutputStream`.
@@ -397,7 +401,7 @@ By default, this converter supports `text/xml` and `application/xml`.
397
401
|===
398
402
399
403
By default, `RestClient` and `RestTemplate` register all built-in message converters, depending on the availability of underlying libraries on the classpath.
400
-
You can also set the message converters to use explicitly, by using `messageConverters` on the `RestClient` builder, or via the `messageConverters` property of `RestTemplate`.
404
+
You can also set the message converters to use explicitly, by using the `messageConverters()` method on the `RestClient` builder, or via the `messageConverters` property of `RestTemplate`.
401
405
402
406
==== Jackson JSON Views
403
407
@@ -437,13 +441,13 @@ parts.add("xmlPart", new HttpEntity<>(myBean, headers));
437
441
----
438
442
439
443
In most cases, you do not have to specify the `Content-Type` for each part.
440
-
The content type is determined automatically based on the `HttpMessageConverter` chosen to serialize it or, in the case of a `Resource` based on the file extension.
444
+
The content type is determined automatically based on the `HttpMessageConverter` chosen to serialize it or, in the case of a `Resource`, based on the file extension.
441
445
If necessary, you can explicitly provide the `MediaType` with an `HttpEntity` wrapper.
442
446
443
-
Once the `MultiValueMap` is ready, you can use it as the body of a POST request, using `RestClient.post().body(parts)` (or `RestTemplate.postForObject`).
447
+
Once the `MultiValueMap` is ready, you can use it as the body of a `POST` request, using `RestClient.post().body(parts)` (or `RestTemplate.postForObject`).
444
448
445
449
If the `MultiValueMap` contains at least one non-`String` value, the `Content-Type` is set to `multipart/form-data` by the `FormHttpMessageConverter`.
446
-
If the `MultiValueMap` has `String` values the `Content-Type` defaults to `application/x-www-form-urlencoded`.
450
+
If the `MultiValueMap` has `String` values, the `Content-Type` defaults to `application/x-www-form-urlencoded`.
447
451
If necessary the `Content-Type` may also be set explicitly.
448
452
449
453
[[rest-request-factories]]
@@ -453,11 +457,11 @@ To execute the HTTP request, `RestClient` uses a client HTTP library.
453
457
These libraries are adapted via the `ClientRequestFactory` interface.
454
458
Various implementations are available:
455
459
456
-
* `JdkClientHttpRequestFactory` for Java's `HttpClient`,
457
-
* `HttpComponentsClientHttpRequestFactory` for use with Apache HTTP Components `HttpClient`,
458
-
* `JettyClientHttpRequestFactory` for Jetty's `HttpClient`,
459
-
* `ReactorNettyClientRequestFactory` for Reactor Netty's `HttpClient`,
460
-
* `SimpleClientHttpRequestFactory` as a simple default.
460
+
* `JdkClientHttpRequestFactory` for Java's `HttpClient`
461
+
* `HttpComponentsClientHttpRequestFactory` for use with Apache HTTP Components `HttpClient`
462
+
* `JettyClientHttpRequestFactory` for Jetty's `HttpClient`
463
+
* `ReactorNettyClientRequestFactory` for Reactor Netty's `HttpClient`
464
+
* `SimpleClientHttpRequestFactory` as a simple default
461
465
462
466
463
467
If no request factory is specified when the `RestClient` was built, it will use the Apache or Jetty `HttpClient` if they are available on the classpath.
@@ -473,12 +477,12 @@ synchronous, asynchronous, and streaming scenarios.
473
477
474
478
`WebClient` supports the following:
475
479
476
-
* Non-blocking I/O.
477
-
* Reactive Streams back pressure.
478
-
* High concurrency with fewer hardware resources.
479
-
* Functional-style, fluent API that takes advantage of Java 8 lambdas.
480
-
* Synchronous and asynchronous interactions.
481
-
* Streaming up to or streaming down from a server.
480
+
* Non-blocking I/O
481
+
* Reactive Streams back pressure
482
+
* High concurrency with fewer hardware resources
483
+
* Functional-style, fluent API that takes advantage of Java 8 lambdas
484
+
* Synchronous and asynchronous interactions
485
+
* Streaming up to or streaming down from a server
482
486
483
487
See xref:web/webflux-webclient.adoc[WebClient] for more details.
484
488
@@ -848,17 +852,17 @@ It can be used to migrate from the latter to the former.
0 commit comments