gpt4 book ai didi

java - Rest 模板 :Could not extract response: no suitable HttpMessageConverter found for response type when we got xml response, 未绑定(bind)到 JAVA 对象

转载 作者:行者123 更新时间:2023-12-01 09:13:52 33 4
gpt4 key购买 nike

在 Spring 应用程序中,我正在调用第三方服务,我正在发送 XML 请求并获取 XML 响应,当无法将该响应解析为 Java 对象时,我正确地获得了 XML 响应,我收到以下错误:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.drf.fundingapi.model.response.pojo.Fmxresponse] and content type [text/html;charset=ISO-8859-1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:884) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:868) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:622) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:498) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at com.drf.fundingapi.apiclient.RestTemplateBase.performRequest(RestTemplateBase.java:17) ~[classes/:na]
at com.drf.fundingapi.apiclient.ApiClient.performPost(ApiClient.java:64) ~[classes/:na]
at com.drf.fundingapi.service.FundingService.getAccountBalanceRequest(FundingService.java:255) ~[classes/:na]
at com.drf.fundingapi.controller.FundingController.getGeneralOperationBalance(FundingController.java:100) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220) ~[spring-web-4.3.4.RELEASE.jar:4.3.4.RELEASE]

我正在发出如下 xml 请求,

Fmxresponse fmxresponse  = apiClient.performPost(url, MediaType.APPLICATION_XML_VALUE, requestData, new HashMap<String, String>(), Fmxresponse.class);

Fmxresponse对象如下,

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "fmxresponse")
public class Fmxresponse implements Serializable {
private static final long serialVersionUID = -4050582129050191456L;
@XmlElement(name = "response")
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
@Override
public String toString() {
return "ClassPojo [response = " + response + "]";
}
}

在应用程序配置中

   @Bean
public RestTemplate getRestTemplate(){
RestTemplate restTemplate = new RestTemplate();

我们收到以下 XML 响应,

<?xml version="1.0" encoding="UTF-8"?>
<fmxresponse>
<response>
<error>
<code>0</code>
<mesg></mesg>
</error>
<category>generaloperation</category>
<function>balance</function>
<result>
<balance>
<type>current</type>
<amount>50,000.00</amount>
</balance>
<balance>
<type>available</type>
<amount>50,000.00</amount>
</balance>
</result>
</response>
</fmxresponse>

任何人都可以知道这里出了什么问题吗?

最佳答案

看起来您的响应返回为 text/html,而不是 text/xml

... and content type [text/html;charset=ISO-8859-1]

您应该创建一个 HttpMessageConverter 来处理此内容类型或配置您的 RestTemplate 以正确处理响应。

参见Force Spring RestTemplate to use XmlConverter有关如何配置 RestTemplate 实例的示例。

关于java - Rest 模板 :Could not extract response: no suitable HttpMessageConverter found for response type when we got xml response, 未绑定(bind)到 JAVA 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40726145/

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