question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clarify behavior for WebClient statusHandlers

See original GitHub issue

During Spring Boot 2.2 upgrade (from 2.1.x) we’ve noticed regression in tests using webclient.

Scenario: Call an enpoint returning 404 NOT_FOUND (with non-empty response body)

Expected: webclient returns empty Mono instance

spring-webflux >= 5.2.0 Actual: webclient call returns an non-empty response but the wrapped object has nothing more than nulls inside - unit test fails.

spring-webflux == 5.1.14 Actual: webclient call returns an empty response - unit test works fine.

Here is a sample that reproduces the problem: https://github.com/jmayday/emptymono Please have a look at this unit test (it uses Wiremock) - shouldReturnEmptyObjectFor404WithResponseBody

Unit test result:

java.lang.AssertionError: 
Expecting an empty Optional but was containing value: <MyEntity(id=null, name=null, owner=null)>.

Pay attention, that if we’ll modify response body in wiremock mapping and replace body

{"error_code":404,"message":"Page not found"}

with empty body, then test will work.

Is this a spring-webflux 5.2.x regression or I’m misusing it?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rstoyanchevcommented, Mar 20, 2020

@jmayday see the updated to the Javadoc in the referenced commit above. I’ve clarified the various cases and provides suggestions for your case.

1reaction
poutsmacommented, Mar 20, 2020

I think you’d have to use an ExchangeFilterFunction to accomplish this. The onStatus handlers are designed to map to an exception; not to an alternative (empty) result.

As said in the commit message, returning an empty result prior to a9b3d95 (eg 5.1.14) will actually leak memory (i.e. the body), so it never “worked” correctly in the first place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebClient Error Handling made Easy | by Sudharshan S.R.
There are various ways in which you can handle errors while using a webclient. I'll explain the easiest ones and let you guys...
Read more >
spring-projects/spring-framework v5.2.5.RELEASE on GitHub
... Improve Javadoc in AnnotatedElementUtils #24740; Clarify behavior for WebClient statusHandlers #24736; Enhance Spring Core documentation, section 1.9.2.
Read more >
Spring Webflux : Webclient : Get body on error - Stack Overflow
Show activity on this post. The retrieve() method in WebClient throws a WebClientResponseException whenever a response with status code 4xx or ...
Read more >
WebClient.ResponseSpec (Spring Framework 6.0.2 API)
Provide a function to map specific error status codes to an error signal to be propagated downstream instead of the response.
Read more >
Spring Webclient : Lecture 1 - Perform HTTP GET, POST, PUT ...
In this lecture, we will code and explore how to Build RESTFUL API clients using Spring WebClient.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found