Skip to content
Advertisement

Reactive WebClient GET Request with text/html response

Currently I’m having an issue with new Spring 5 WebClient and I need some help to sort it out. The issue is:

I request some url that returns json response with content type text/html;charset=utf-8.

But unfortunately I’m still getting an exception: org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type ‘text/html;charset=utf-8’ not supported. So I can’t convert response to DTO.

For request I use following code:

JavaScript

Btw, it really doesn’t matter which type I point in accept header, always returning text/html. So how could I get my response converted eventually?

Advertisement

Answer

Having a service send JSON with a "text/html" Content-Type is rather unusual.

There are two ways to deal with this:

  1. configure the Jackson decoder to decode "text/html" content as well; look into the WebClient.builder().exchangeStrategies(ExchangeStrategies) setup method
  2. change the “Content-Type” response header on the fly

Here’s a proposal for the second solution:

JavaScript

Note that you should only use that client in that context (for this host). I’d strongly suggest to try and fix that strange content-type returned by the server, if you can.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement