WebClient's bodyToMono error

Viewed 11772

I am making an external api call with Spring 5 WebClient and would like to map the response to an Object like this:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Response {
    private long length;
}

private Mono<Response> getResponse() {
    return webClient.get()
            .uri("someURI")
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .retrieve()
            .bodyToMono(Response.class);
}

But I get an error:

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported

However if I extract response body to String like this:

private Mono<String> getResponse() {
return webClient.get()
        .uri("someURI")
        .accept(MediaType.APPLICATION_JSON_UTF8)
        .retrieve()
        .bodyToMono(String.class); 
}

Then it works properly. Any ideas how to solve this?

EDIT:

Response body:

{
  "blocks": [
  {
  "height": 545551,
  "size": 48289,
  "virtualSize": 48289,
  "hash": "000000000000000000541d265115ec188544420c4b0e5dff6f2171e17e4991c9",
  "time": 1535551238,
  "txlength": 80,
  "poolInfo": {}
  }
  ],
  "length": 1,
  "pagination": {
  "next": "2018-08-30",
  "prev": "2018-08-28",
  "currentTs": 1535587199,
  "current": "2018-08-29",
  "isToday": true,
  "more": true,
  "moreTs": 1535587200
  }
}

Headers:

status: 200 OK
1 Answers

If the HTTP response doesn't have a Content-Type header, the client will assume application/octet-stream, which is not a Content-Type that's supported by the Jackson codec in Spring WebFlux.

So the behavior you're seeing is the expected one, and the web API should be fixed to add a response header.

Related