Skip to content

Deprecate WebClient#exchange() and provide safer alternatives #25751

@rstoyanchev

Description

@rstoyanchev

exchange() provides more flexibility than retrieve() but leaves the full handling of the response to the application and it remains difficult to use it correctly in order to avoid memory and connection leaks. retrieve() is safer because it wraps the handling of the response (and the framework can ensure correct release of resources) and since 5.2 with access to the response status and headers via ResponseEntity, retrieve() can cover all of common use cases.

The actual reasons for using exchange() are few. One example is the ability to suppress an error status and produce either a typed object from a successful response or a different object from an error response with the output consumed as Mono<Object>. However exchange() continues to be used much more widely than that.

It is time to provide a safer alternative to exchange() that retains the same flexibility but allows the WebClient to be involved in the handling of the response. For example:

<V> Mono<V> exchangeToMono(Function<ClientResponse, ? extends Mono<V>> responseHandler);

<V> Flux<V> exchangeToFlux(Function<ClientResponse, ? extends Flux<V>> responseHandler);

We can then deprecate exchange() and remove it in the next major version.

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