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

标签 java xml spring resttemplate

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

相关文章:

java - 如何从 Android 中的 JSON 解析中深入提取对象/数组

java - Eclipse 在 processWorkerExit 上中断

java - 在多个应用服务器中生成唯一的18到25大小的随机数

java - 将多个 dao 注入(inject)到一个服务中

python - 如何在 Python 中使用 OpenSSL 从 pfx 文件中提取 key ?

spring - 保持 Spring Security session 超时的区域设置

java - 如何排列 JComboBox 项目

java - 将Web浏览器引擎集成到应用程序中,最好使用Java

Python ElementTree 使用特殊字符 '\x0b' 生成格式不正确的 XML 文件

xml - 在emacs中用一个键将html标签包裹在下一个单词周围