How to pass Error Body and StatusCode using Spring WebClient

63 Views Asked by At

I have a request like this:

    public ResponseEntity<JsonNode> test(String id) {
    return WebClient
        .create("http://localhost:8090/database/api")
        .get()
        .uri("/test/{id}", id)
        .accept(MediaType.APPLICATION_JSON)
        .exchange()
        .flatMap(resp -> resp.toEntity(JsonNode.class))
        .block();
}

Works fine when request is ok (HttpStatus code 200)

How I return the same Body and HttpStatus Code when error happen

1

There are 1 best solutions below

0
On

I solved it this way:

    public ResponseEntity<JsonNode> test(String id) {
    return WebClient.create("http://localhost:8090/database/api").get().uri("/test/{id}", id)
            .accept(MediaType.ALL).exchange()
            .block()
            .toEntity(JsonNode.class)
            .block();
}

The problem was the lack of a block () after exchange().