spring-webclient - 嵌套异常是 web.reactive.function.UnsupportedMediaTypeException : Content type 'text/html;charset=utf-8' not supported for bodyType

标签 spring-webclient

创建了一个 REST Web 服务(服务 1),它尝试使用另一个返回 Content-Type =“text/html;charset=utf-8”的服务(服务 2)

我的 WebClient 代码使用另一个服务:

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;
    }

我收到以下错误:

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]
    

响应Pojo类:

@Data
public class ServiceResponse{

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

我收到的回复:

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

有人可以推荐我吗?

最佳答案

由于您的响应是 HTML,因此没有内置方法将其直接转换为 POJO,因此您必须使用原始响应,例如作为字符串或字节缓冲区。

bodyToMono 中的响应类指定为 String.class,然后使用选择的库手动解析 HTML。您发布的 HTML 看起来符合 XML(即 XHTML),因此这可能会使事情变得更加容易,但仅限于您实际提取要查找的字符串的部分。

关于spring-webclient - 嵌套异常是 web.reactive.function.UnsupportedMediaTypeException : Content type 'text/html;charset=utf-8' not supported for bodyType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69228765/

相关文章:

java - 如何使用 spring webClient 等待所有 http 请求完成?

spring-webflux - Spring Web 客户端 : Call method in retry

spring-boot - 如何使用 spring boot web-client 为内容类型 application/x-www-form-urlencoded 的表单数据发布请求

spring-boot - 如何并行调用多个Spring Webclient并等待结果?

java - 如何将一系列整数从一个微服务发送到另一个微服务

java - 配置 Spring WebClient 代理时如何更改 DEFAULT_CONNECT_TIMEOUT_MILLIS ?

spring-webflux - Spring WebFlux - 通过 webClient 转发 FilePart

java - 如何在 Spring WebClient 中捕获 ConnectionException?

java - Spring WebClient调用两个依赖的API

java - 如何并行调用多个http请求(具有重试功能)并等待所有请求完成?