Polishing contribution

Closes gh-31991
This commit is contained in:
rstoyanchev 2024-01-10 12:16:19 +00:00
parent 864b1c95cd
commit 6dca7b28cc
1 changed files with 29 additions and 23 deletions

View File

@ -1078,31 +1078,34 @@ underlying HTTP client, which operates at a lower level and provides more contro
[[rest-http-interface-exceptions]] [[rest-http-interface-exceptions]]
=== Exception Handling === Error Handling
In order to provide a custom way of handling errors, you can register response
status handlers on the underlying HTTP clients. To customize error response handling, you need to configure the underlying HTTP client.
For `RestClient`: For `RestClient`:
By default, `RestClient` raises `RestClientException` for 4xx and 5xx HTTP status codes. To customize this, you can register a response status handler that applies to all responses performed through the client: By default, `RestClient` raises `RestClientException` for 4xx and 5xx HTTP status codes.
To customize this, register a response status handler that applies to all responses
performed through the client:
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
RestClient restClient = RestClient.builder() RestClient restClient = RestClient.builder()
.defaultStatusHandler(HttpStatusCode::isError, .defaultStatusHandler(HttpStatusCode::isError, (request, response) -> ...)
(request, response) -> ...) .build();
.build();
RestClientAdapter clientAdapter = RestClientAdapter.create(restClient); RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
.builderFor(clientAdapter).build();
---- ----
For more details and options, such as suppressing error status codes, see the Javadoc of `defaultStatusHandler` in `RestClient.Builder`. For more details and options, such as suppressing error status codes, see the Javadoc of
`defaultStatusHandler` in `RestClient.Builder`.
For `WebClient`: For `WebClient`:
By default, `WebClient` raises `WebClientResponseException` for 4xx and 5xx HTTP status codes. To customize this, you can register a response status handler that applies to all responses performed through the client: By default, `WebClient` raises `WebClientResponseException` for 4xx and 5xx HTTP status codes.
To customize this, register a response status handler that applies to all responses
performed through the client:
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
@ -1110,25 +1113,28 @@ By default, `WebClient` raises `WebClientResponseException` for 4xx and 5xx HTTP
.defaultStatusHandler(HttpStatusCode::isError, resp -> ...) .defaultStatusHandler(HttpStatusCode::isError, resp -> ...)
.build(); .build();
WebClientAdapter clientAdapter = WebClientAdapter.create(webClient); WebClientAdapter adapter = WebClientAdapter.create(webClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(adapter).build();
.builder(clientAdapter).build();
---- ----
For more details and options, such as suppressing error status codes, see the Javadoc of `defaultStatusHandler` in `WebClient.Builder`. For more details and options, such as suppressing error status codes, see the Javadoc of
`defaultStatusHandler` in `WebClient.Builder`.
For `RestTemplate`: For `RestTemplate`:
By default, `RestTemplate` raises `RestClientException` for 4xx and 5xx HTTP status codes. To customize this, you can register an error handler that applies to all responses performed through the client: By default, `RestTemplate` raises `RestClientException` for 4xx and 5xx HTTP status codes.
To customize this, register an error handler that applies to all responses
performed through the client:
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(aCustomErrorHandler); restTemplate.setErrorHandler(myErrorHandler);
RestTemplateAdapter clientAdapter = RestTemplateAdapter.create(restTemplate); RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);
HttpServiceProxyFactory factory = HttpServiceProxyFactory HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
.builderFor(clientAdapter).build();
---- ----
For more details and options, see the Javadoc of `setErrorHandler` in `RestTemplate`. For more details and options, see the Javadoc of `setErrorHandler` in `RestTemplate` and
the `ResponseErrorHandler` hierarchy.