Skip to content

Commit 18dc7d7

Browse files
committed
Merge branch '6.1.x'
2 parents 17b2087 + 5830aac commit 18dc7d7

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

framework-docs/modules/ROOT/pages/integration/rest-clients.adoc

+37-33
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ The Spring Framework provides the following choices for making calls to REST end
1313
== `RestClient`
1414

1515
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.
1717

1818
=== Creating a `RestClient`
1919

2020
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.
2222

2323
Once created (or built), the `RestClient` can be used safely by multiple threads.
2424

@@ -64,35 +64,36 @@ val customClient = RestClient.builder()
6464
=== Using the `RestClient`
6565

6666
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.
6868

6969
==== Request URL
7070

7171
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.
7474
String URLs are encoded by default, but this can be changed by building a client with a custom `uriBuilderFactory`.
7575

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.
7777
For more details on working with and encoding URIs, see xref:web/webmvc/mvc-uri-building.adoc[URI Links].
7878

7979
==== Request headers and body
8080

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)`.
8383

8484
The request body itself can be set by `body(Object)`, which internally uses <<rest-message-conversion>>.
8585
Alternatively, the request body can be set using a `ParameterizedTypeReference`, allowing you to use generics.
8686
Finally, the body can be set to a callback function that writes to an `OutputStream`.
8787

8888
==== Retrieving the response
89+
8990
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 typesfor instance, bytes can be converted into a `String`, JSON can be converted into objects using Jackson, and so on (see <<rest-message-conversion>>).
9293

9394
The response can also be converted into a `ResponseEntity`, giving access to the response headers as well as the body.
9495

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.
9697

9798
[tabs]
9899
======
@@ -171,7 +172,7 @@ println("Contents: " + result.body) <3>
171172
======
172173

173174
`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.
175176

176177
[tabs]
177178
======
@@ -248,8 +249,9 @@ val response = restClient.post() <2>
248249
======
249250

250251
==== Error handling
252+
251253
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`.
253255

254256
[tabs]
255257
======
@@ -286,8 +288,9 @@ val result = restClient.get() <1>
286288
======
287289

288290
==== 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.
291294

292295
[tabs]
293296
======
@@ -336,6 +339,7 @@ val result = restClient.get()
336339

337340
[[rest-message-conversion]]
338341
=== HTTP Message Conversion
342+
339343
[.small]#xref:web/webflux/reactive-spring.adoc#webflux-codecs[See equivalent in the Reactive stack]#
340344

341345
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`.
397401
|===
398402

399403
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`.
401405

402406
==== Jackson JSON Views
403407

@@ -437,13 +441,13 @@ parts.add("xmlPart", new HttpEntity<>(myBean, headers));
437441
----
438442

439443
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.
441445
If necessary, you can explicitly provide the `MediaType` with an `HttpEntity` wrapper.
442446

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`).
444448

445449
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`.
447451
If necessary the `Content-Type` may also be set explicitly.
448452

449453
[[rest-request-factories]]
@@ -453,11 +457,11 @@ To execute the HTTP request, `RestClient` uses a client HTTP library.
453457
These libraries are adapted via the `ClientRequestFactory` interface.
454458
Various implementations are available:
455459

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
461465

462466

463467
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.
473477

474478
`WebClient` supports the following:
475479

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
482486

483487
See xref:web/webflux-webclient.adoc[WebClient] for more details.
484488

@@ -848,17 +852,17 @@ It can be used to migrate from the latter to the former.
848852
.toEntity(ParameterizedTypeReference)` footnote:request-entity[]
849853

850854

851-
| `execute(String, HttpMethod method, RequestCallback, ResponseExtractor, Object...)`
855+
| `execute(String, HttpMethod, RequestCallback, ResponseExtractor, Object...)`
852856
| `method(HttpMethod)
853857
.uri(String, Object...)
854858
.exchange(ExchangeFunction)`
855859

856-
| `execute(String, HttpMethod method, RequestCallback, ResponseExtractor, Map)`
860+
| `execute(String, HttpMethod, RequestCallback, ResponseExtractor, Map)`
857861
| `method(HttpMethod)
858862
.uri(String, Map)
859863
.exchange(ExchangeFunction)`
860864

861-
| `execute(URI, HttpMethod method, RequestCallback, ResponseExtractor)`
865+
| `execute(URI, HttpMethod, RequestCallback, ResponseExtractor)`
862866
| `method(HttpMethod)
863867
.uri(URI)
864868
.exchange(ExchangeFunction)`

0 commit comments

Comments
 (0)