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.

Provide a way to automatically deserialize non-OK JSON response using `WebClient` and `RestClient`

See original GitHub issue

WebClient 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:open
  • Created a year ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ikhooncommented, Sep 7, 2022

Thanks for the pull request. I will take a look at it soon.

1reaction
ikhooncommented, Sep 1, 2022

We can implement HttpStatusPredicate like the following.

final class HttpStatusPredicate extends Predicate<HttpStatus> {

    @Nullable
    private HttpStatus status;
    @Nullable
    private HttpStatusClass statusClass;
     
    ...    

    @Override
    boolean test(HttpStatus status) {
        if (this.status != null) {
            return this.status.equals(status);
        }
        assert statusClass != null;
        return status.codeClass() == statusClass;
    }

    @Nullable
    HttpStatus status() {
        return status;
    }

    @Nullable
    HttpStatusClass statusClass() {
        return statusClass;
    }
}

The custom HttpStatusPredicate will carry the desired status information, and we can refactor AggregatedResponseAs to use HttpStatusPredicate for a simple condition.

// In AggregatedResponseAs
private static final HttpStatusPredicate SUCCESS_PREDICATE = new HttpStatusPredicate(HttpStatusClass.SUCCESS);

private static <T> ResponseEntity<T> 
    newJsonResponseEntity(AggregatedHttpResponse response, 
                          JsonDecoder<T> decoder) {
     return newJsonResponseEntity(response, decoder, SUCCESS_PREDICATE);
}

private static <T> ResponseEntity<T> 
    newJsonResponseEntity(AggregatedHttpResponse response, 
                          JsonDecoder<T> decoder, HttpStatus status) {
     return newJsonResponseEntity(response, decoder, new HttpStatusPredicate(status));
}

private static <T> ResponseEntity<T> 
    newJsonResponseEntity(AggregatedHttpResponse response, 
                          JsonDecoder<T> decoder, Predicate<HttpStatus> status) {
    if (status instanceof HttpStatusPredicate) {
       ... // Extract either HttpStatus or HttpStatusClass to provide the expected status.
    }
}
Read more comments on GitHub >

github_iconTop 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 >

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