Skip to content

Revisit handling of 4xx and 5xx status codes in WebClient [SPR-15486] #20046

@spring-projects-issues

Description

@spring-projects-issues

Arjen Poutsma opened SPR-15486 and commented

Currently, we throw a WebClientException for 4xx and 5xx status codes in ClientResponse.bodyToMono(Class) and ClientResponse.bodyToFlux(Class) (but not ClientResponse.body(BodyExtractor). The underlying thought here was that the response format of a 4xx or 5xx error typically does not take the same form as a "regular" 2xx response. Now that we have the ResponseSpec in WebClient, let's revisit this handling of 4xx and 5xx status codes on the client side.

I think the rule to throwing an exception should be simple: if there is no way for the user to get the response status code, then a WebClientException should be returned. If there is a way to get to the status code, then we should not return an exception.

In our current WebClient, this rule would bring the following changes:

  1. WebClient.ResponseSpec.bodyToMono and bodyToFlux should return WebClientException when the response status code is 4xx or 5xx.
  2. WebClient.ResponseSpec.bodyToEntity and bodyToEntityList should not return an exception in these cases, because you can get access to the status code through ResponseEntity.getStatusCode().
  3. ClientResponse.bodyToMono and bodyToFlux should not return an exception, as the ClientResponse exposes the response status code through statusCode().

All other WebClient/ClientResponse methods should keep their current behaviour.


Issue Links:

Referenced from: commits 4a8c99c, 0e7d6fc

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions