Skip to content

Commit 24c4614

Browse files
committed
Add docs on exceptions for HTTP interface client
Closes gh-28533
1 parent 1aaa44b commit 24c4614

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.ResponseEntity;
2727
import org.springframework.util.Assert;
28-
import org.springframework.web.reactive.function.client.ClientResponse;
2928
import org.springframework.web.reactive.function.client.WebClient;
3029
import org.springframework.web.service.invoker.HttpClientAdapter;
3130
import org.springframework.web.service.invoker.HttpRequestValues;
@@ -57,41 +56,41 @@ private WebClientAdapter(WebClient webClient) {
5756

5857
@Override
5958
public Mono<Void> requestToVoid(HttpRequestValues requestValues) {
60-
return toBodySpec(requestValues).exchangeToMono(ClientResponse::releaseBody);
59+
return newRequest(requestValues).retrieve().toBodilessEntity().then();
6160
}
6261

6362
@Override
6463
public Mono<HttpHeaders> requestToHeaders(HttpRequestValues requestValues) {
65-
return toBodySpec(requestValues).retrieve().toBodilessEntity().map(ResponseEntity::getHeaders);
64+
return newRequest(requestValues).retrieve().toBodilessEntity().map(ResponseEntity::getHeaders);
6665
}
6766

6867
@Override
6968
public <T> Mono<T> requestToBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
70-
return toBodySpec(requestValues).retrieve().bodyToMono(bodyType);
69+
return newRequest(requestValues).retrieve().bodyToMono(bodyType);
7170
}
7271

7372
@Override
7473
public <T> Flux<T> requestToBodyFlux(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
75-
return toBodySpec(requestValues).retrieve().bodyToFlux(bodyType);
74+
return newRequest(requestValues).retrieve().bodyToFlux(bodyType);
7675
}
7776

7877
@Override
7978
public Mono<ResponseEntity<Void>> requestToBodilessEntity(HttpRequestValues requestValues) {
80-
return toBodySpec(requestValues).retrieve().toBodilessEntity();
79+
return newRequest(requestValues).retrieve().toBodilessEntity();
8180
}
8281

8382
@Override
8483
public <T> Mono<ResponseEntity<T>> requestToEntity(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
85-
return toBodySpec(requestValues).retrieve().toEntity(bodyType);
84+
return newRequest(requestValues).retrieve().toEntity(bodyType);
8685
}
8786

8887
@Override
8988
public <T> Mono<ResponseEntity<Flux<T>>> requestToEntityFlux(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
90-
return toBodySpec(requestValues).retrieve().toEntityFlux(bodyType);
89+
return newRequest(requestValues).retrieve().toEntityFlux(bodyType);
9190
}
9291

9392
@SuppressWarnings("ReactiveStreamsUnusedPublisher")
94-
private WebClient.RequestBodySpec toBodySpec(HttpRequestValues requestValues) {
93+
private WebClient.RequestBodySpec newRequest(HttpRequestValues requestValues) {
9594

9695
HttpMethod httpMethod = requestValues.getHttpMethod();
9796
Assert.notNull(httpMethod, "HttpMethod is required");

src/docs/asciidoc/integration.adoc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ methods for HTTP exchanges. You can then generate a proxy that implements this i
371371
and performs the exchanges. This helps to simplify HTTP remote access which often
372372
involves a facade that wraps the details of using the underlying HTTP client.
373373

374-
To start, declare an interface with annotated, HTTP exchange methods:
374+
One, declare an interface with `@HttpExchange` methods:
375375

376376
[source,java,indent=0,subs="verbatim,quotes"]
377377
----
@@ -385,8 +385,7 @@ To start, declare an interface with annotated, HTTP exchange methods:
385385
}
386386
----
387387

388-
Now you create a proxy for the interface that performs the declared exchanges through
389-
the `WebClient`:
388+
Two, create a proxy that will perform the declared HTTP exchanges:
390389

391390
[source,java,indent=0,subs="verbatim,quotes"]
392391
----
@@ -396,7 +395,7 @@ the `WebClient`:
396395
RepositoryService service = factory.createClient(RepositoryService.class);
397396
----
398397

399-
An HTTP service interface can declare common attributes at the type level:
398+
`@HttpExchange` is supported at the type level where it applies to all methods:
400399

401400
[source,java,indent=0,subs="verbatim,quotes"]
402401
----
@@ -504,6 +503,27 @@ TIP: You can also use any other async or reactive types registered in the
504503
`ReactiveAdapterRegistry`.
505504

506505

506+
[[rest-http-interface-exceptions]]
507+
==== Exception Handling
508+
509+
By default, `WebClient` raises `WebClientResponseException` for 4xx and 5xx HTTP status
510+
codes. To customize this, you can register a response status handler that applies to all
511+
responses performed through the client:
512+
513+
[source,java,indent=0,subs="verbatim,quotes"]
514+
----
515+
WebClient webClient = WebClient.builder()
516+
.defaultStatusHandler(HttpStatusCode::isError, resp -> ...)
517+
.build();
518+
519+
HttpServiceProxyFactory proxyFactory =
520+
WebClientAdapter.createHttpServiceProxyFactory(webClient);
521+
----
522+
523+
For more details and options, such as suppressing error status codes, see the Javadoc of
524+
`defaultStatusHandler` in `WebClient.Builder`.
525+
526+
507527

508528

509529
[[jms]]

0 commit comments

Comments
 (0)