gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:16:32 25 4
gpt4 key购买 nike

创建了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com