Skip to content

Commit 864b1c9

Browse files
OlgaMaciaszekrstoyanchev
authored andcommitted
Document exception handling for RestClient and RestTemplate-backed interface clients.
1 parent 168c60c commit 864b1c9

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,10 +1079,30 @@ underlying HTTP client, which operates at a lower level and provides more contro
10791079

10801080
[[rest-http-interface-exceptions]]
10811081
=== Exception Handling
1082+
In order to provide a custom way of handling errors, you can register response
1083+
status handlers on the underlying HTTP clients.
10821084

1083-
By default, `WebClient` raises `WebClientResponseException` for 4xx and 5xx HTTP status
1084-
codes. To customize this, you can register a response status handler that applies to all
1085-
responses performed through the client:
1085+
For `RestClient`:
1086+
1087+
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:
1088+
1089+
[source,java,indent=0,subs="verbatim,quotes"]
1090+
----
1091+
RestClient restClient = RestClient.builder()
1092+
.defaultStatusHandler(HttpStatusCode::isError,
1093+
(request, response) -> ...)
1094+
.build();
1095+
1096+
RestClientAdapter clientAdapter = RestClientAdapter.create(restClient);
1097+
HttpServiceProxyFactory factory = HttpServiceProxyFactory
1098+
.builderFor(clientAdapter).build();
1099+
----
1100+
1101+
For more details and options, such as suppressing error status codes, see the Javadoc of `defaultStatusHandler` in `RestClient.Builder`.
1102+
1103+
For `WebClient`:
1104+
1105+
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:
10861106

10871107
[source,java,indent=0,subs="verbatim,quotes"]
10881108
----
@@ -1095,5 +1115,20 @@ responses performed through the client:
10951115
.builder(clientAdapter).build();
10961116
----
10971117

1098-
For more details and options, such as suppressing error status codes, see the Javadoc of
1099-
`defaultStatusHandler` in `WebClient.Builder`.
1118+
For more details and options, such as suppressing error status codes, see the Javadoc of `defaultStatusHandler` in `WebClient.Builder`.
1119+
1120+
For `RestTemplate`:
1121+
1122+
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:
1123+
1124+
[source,java,indent=0,subs="verbatim,quotes"]
1125+
----
1126+
RestTemplate restTemplate = new RestTemplate();
1127+
restTemplate.setErrorHandler(aCustomErrorHandler);
1128+
1129+
RestTemplateAdapter clientAdapter = RestTemplateAdapter.create(restTemplate);
1130+
HttpServiceProxyFactory factory = HttpServiceProxyFactory
1131+
.builderFor(clientAdapter).build();
1132+
----
1133+
1134+
For more details and options, see the Javadoc of `setErrorHandler` in `RestTemplate`.

0 commit comments

Comments
 (0)