Rest模板:无法提取响应:当我们得到xml响应时没有为响应类型找到合适的HttpMessageConverter,而不是绑定到JAVA对象

时间:2016-11-21 17:38:33

标签: java xml spring resttemplate

在Spring应用程序中,我正在调用第三方服务,我正在向我发送XML请求并获得XML响应,当无法将该响应解析为Java Object时,我得到了正确的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 + "]";
    }
}

在App.Config中

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

任何人都有任何想法,这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

您的回复看起来像text/html,而不是text/xml

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

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

有关如何配置RestTemplate实例的详细信息,请参阅Force Spring RestTemplate to use XmlConverter