0

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?

user1310038
  • 97
  • 2
  • 3
  • 10

1 Answers1

1

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.

Community
  • 1
  • 1
Adam
  • 2,214
  • 1
  • 15
  • 26
  • Thanks for quick response, but still I am getting, org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [java.lang.String] and content type [application/xml] – user1310038 Nov 21 '16 at 20:34
  • That is a pretty different error. The one in your question was about reading a response, and this one is about writing a request. It says you cannot write a `String` as `application/xml`. What did you add and where are you getting this error? – Adam Nov 21 '16 at 20:58
  • I have added @Bean public RestTemplate getRestTemplate() { RestTemplate restTemplate = new RestTemplate(); List> messageConverters = new ArrayList>(); Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter(); List mediaTypes = new ArrayList(); mediaTypes.add(MediaType.TEXT_HTML); jaxbMessageConverter.setSupportedMediaTypes(mediaTypes); messageConverters.add(jaxbMessageConverter); restTemplate.setMessageConverters(messageConverters); return restTemplate; } – user1310038 Nov 21 '16 at 22:55
  • Are you using the same `RestTemplate` to make the request? – Adam Nov 22 '16 at 15:25
  • Yes. RestTemplate using to make request. – user1310038 Nov 22 '16 at 15:32
  • Try also adding `mediaTypes.add(MediaType.APPLICATION_XML);` – Adam Nov 22 '16 at 15:42
  • Still not working. Its pretty simple but still not working strange! – user1310038 Nov 22 '16 at 23:33
  • @user1310038 Running into something similar.. how did you resolve this? – activelearner Mar 02 '20 at 22:07