-
Notifications
You must be signed in to change notification settings - Fork 38.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate WebClient#exchange() and provide safer alternatives #25751
Comments
The exchange() method is now deprecated because it is not safe for general use but that doesn't apply to the WebTestClient because it exposes a different higher level API for response handling that ensures the response is consumed. Nevertheless WebTestClient cannot call WebClient.exchange() any more. To fix this WebTestClient no longer delegates to WebClient and thus gains direct access to the underlying ExchangeFunction. This is not a big deal because WebClient and WebTestClient do the same only when it comes to gathering builder options and request input. See gh-25751
* Adapt to WebFlux deprecations: spring-projects/spring-framework#25751 * Back to SNAPSHOTs * Fix new Sonar smells
Re-opening in order to also consider if we need any updates related to Kotlin. |
I think we need |
Hello! |
@mplain as mentioned in the deprecation notice the preferred replacement is |
@rstoyanchev I'm not sure I should decode the body to byte[], I just need to forward the response from the other server to my client. Is there a better way to do it, without decoding the body at my proxy server? |
You can decode to |
@rstoyanchev meanwhile is there any way to combine
I assume joining DataBuffers would be pretty much the same as using |
Correct. |
As part of the unit testing, how can I mock the |
I am able to achieve it using Mockito.doAnswer() option from mockito framework. Please ignore my above question. Thanks! Working sample code for reference. Hope someone might find it helpful.
|
I'm not sure mocks are a good fit for mocking server responses. You can use something like OkHttp MockWebServer or WireMock. |
Thanks for your response. Yes we use WireMock for the Component Test. But for the Unit Testing, it will be bit heavy to have stub server. So we've used Mockito to cover that. And above code works just fine. |
exchange()
provides more flexibility thanretrieve()
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 viaResponseEntity
,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 asMono<Object>
. Howeverexchange()
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:We can then deprecate
exchange()
and remove it in the next major version.The text was updated successfully, but these errors were encountered: