Provide a way to automatically deserialize non-OK JSON response using `WebClient` and `RestClient`
See original GitHub issueWebClient
or RestClient
is able to deserialize a JSON response into an object using:
WebClient client = WebClient.of(...);
ComletableFuture<ResponseEntity<MyItems>> response =
client.prepare()
.get("/api/items/...")
.asJson(MyItems.class)
.execute();
The client tries to convert the JSON response into an object when the class of response status is a success. https://github.com/line/armeria/blob/ccf630c417bc2f9f78a90bad53a999cc7aa46314/core/src/main/java/com/linecorp/armeria/client/AggregatedResponseAs.java#L58-L62 I believe that most REST clients expect a 2xx status for normal situations. However, when it comes to tests, it would be inconvenient. Users might want to test an error response.
So it could be useful to add some variant methods of .asJson(...)
of WebClient
and .execute(...)
of RestClient
ComletableFuture<ResponseEntity<MyError>> response =
client.prepare()
.get("/api/items/...?bad=parameter")
.asJson(MyError.class, HttpStatus.INTERNAL_SERVER_ERROR)
.execute();
In addition to HttpStatus
, we can also add Predicate<HttpStatus>
and HttpStatusClass
as a parameter.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Get List of JSON Objects with WebClient - Baeldung
In this article, we'll find out how to convert a JSON Array into a Java Array of Object, Array of POJO, and a...
Read more >Deserialize a json array to objects using Jackson and WebClient
I have this json response from a service: [ { "symbol": "XRPETH", "orderId": 12122, "clientOrderId" ...
Read more >How to Read JSON Data with Spring WebClient - amitph
Covers a different ways of reading JSON data with Spring WebClient and mapping JSON to POJOs, or Objects using Mono and Flux.
Read more >Building a Reactive RESTful Web Service - Spring
This guide shows the functional way of using Spring WebFlux. ... You will build a RESTful web service with Spring Webflux and a...
Read more >Testing Serialization With Spring Boot @JsonTest
Learn how to test JSON serialization with Spring Boot @JsonTest. ... the deserialization of requests and serialization of responses using ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the pull request. I will take a look at it soon.
We can implement
HttpStatusPredicate
like the following.The custom
HttpStatusPredicate
will carry the desired status information, and we can refactor AggregatedResponseAs to useHttpStatusPredicate
for a simple condition.