Search code examples
spring-webclient

nested exception is web.reactive.function.UnsupportedMediaTypeException: Content type 'text/html;charset=utf-8' not supported for bodyType


Created a REST Webservice(Service 1) which tries to consume another service(Service 2) which returns Content-Type = "text/html;charset=utf-8"

my WebClient code that consumes another service:

ublic <T, N> N invokeService(String endPointUrl, HttpMethod httpMethod, HttpHeaders headers, T requestPayload,
            Class<T> requestPlayLoadClass, Class<N> serviceResponseClass) {
    UriSpec<RequestBodySpec> uriSpec = webclient.method(httpMethod);
    RequestBodySpec bodySpec = uriSpec.uri(endPointUrl);
    RequestHeadersSpec<?> headersSpec = buildRequest(headers, requestPayload, requestPlayLoadClass, bodySpec);

    Mono<N> res = headersSpec.retrieve()
                .onStatus(HttpStatus::is4xxClientError, clientResponse -> handle4xxError(clientResponse))
                .onStatus(HttpStatus::is5xxServerError, clientResponse -> handle5xxError(clientResponse))
                .bodyToMono(serviceResponseClass);

    return res.block();

}

private <T> RequestHeadersSpec<?> buildRequest(HttpHeaders headers, T requestPayload, Class<T> requestPlayLoadClass,
            RequestBodySpec bodySpec) {
        RequestHeadersSpec<?> headersSpec = bodySpec.headers(clientHeaders -> clientHeaders.addAll(headers));
        if (requestPayload != null && requestPlayLoadClass != null) {
            headersSpec = bodySpec.body(Mono.just(requestPayload), requestPlayLoadClass);
        }
        return headersSpec;
    }

I am getting the below error:

org.springframework.web.reactive.function.client.WebClientResponseException: nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/html;charset=utf-8' not supported for bodyType
    at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:212) ~[spring-webflux-5.3.6.jar:5.3.6]
    at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:216) ~[spring-webflux-5.3.6.jar:5.3.6]
    at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2397) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:2193) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onSubscribe(FluxOnErrorResume.java:74) ~[reactor-core-3.4.5.jar:3.4.5]
    

Response Pojo class:

@Data
public class ServiceResponse{

    private String name;
    private String systemDevice;
    private String key;
}

the response which I am receiving:

<HTML>

<HEAD>
    <TITLE>Connect Servlet Version 6</TITLE>
</HEAD>

<BODY>
    <H3>Message Response from Connect Servlet for Version 6</H3>
    <BR>Connect Server running servlet <BR>
    <HR>&nbsp;&nbsp;&nbsp;interface:
    IManagement<br>&nbsp;&nbsp;&nbsp;method: deliverevent<br>&nbsp;&nbsp;&nbsp;contactevent: PauseRecord<br>&nbsp;&nbsp;&nbsp;agent.agent: test<br>&nbsp;&nbsp;&nbsp;agent.systemdevice: Screen Data Source<br>&nbsp;&nbsp;&nbsp;device.systemdevice: test_lab<br>ContactEvent: DEVICE_NOT_IN_CALL
    <HR>
</BODY>

</HTML>

Can anyone please suggest me?


Solution

  • Since your response is HTML, there is no built in way of converting it directly to a POJO, so you will have to consume the response raw, e.g. as string or byte buffer.

    Specify the response class in bodyToMono as String.class, and then parse the HTML manually using a library of choice. The HTML you posted looks XML conform (i.e. XHTML), so that might make things drastically easier, but only up to the part where you actually extract the string that you're looking for.