user1310038
user1310038

Reputation: 97

Rest Template:Could not extract response: no suitable HttpMessageConverter found for response type when we got xml response, not bind to JAVA object

In Spring app, I am calling third party services, I am sending XML request and getting XML response to me, I got the XML response properly when not able to parse that response into Java Object, I am getting the following error:

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]

I am making xml request as follows,

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

Fmxresponse object as follows,

@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 + "]";
    }
}

In App.Config

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

Following XML response we receive,

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

Can any one have any idea, what's going wrong here?

Upvotes: 0

Views: 18067

Answers (1)

Adam
Adam

Reputation: 2269

Looks like your response is coming back as text/html, not text/xml.

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

You should create a HttpMessageConverter to handle this content type or configure your RestTemplate to handle the response properly.

See Force Spring RestTemplate to use XmlConverter for an exammple of how to configure your RestTemplate instance.

Upvotes: 1

Related Questions