15

We are integrating with a third party that is sending xml with the content-type header as text/html. We were planning on using Spring's RestTemplate to map it to classes we've generated from xsds, but the RestTemplate fails to find an appropriate converter to use for the content. The third party refuses to fix the content-type because it might break other partners' integration.

Is there a way with Spring's RestTemplate to force it to use a specific converter? We are basically just doing the following:

RestTemplate restTemplate = new RestTemplate();
XmlClass xmlClass = restTemplate.getForObject("http://example.com/", XmlClass.class);

And get the following exception:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [XmlClass] and content type [text/html;charset=ISO-8859-1] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)

Nathanial
  • 2,177
  • 1
  • 15
  • 34

3 Answers3

22

The solution we implemented was to add a Jaxb2RootElementHttpMessageConverter with MediaType.TEXT_HTML to the RestTemplate HttpMessageConverters. It's not ideal since it creates a redundant jaxb message converter but it works.

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);
Nathanial
  • 2,177
  • 1
  • 15
  • 34
5

I did not see an example posted of how to actually do this with a custom interceptor, so here is one for reference sake:

public class MyXmlInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);
    HttpHeaders headers = response.getHeaders();

    // you'd want to check if the value needs to be changed
    if (headers.containsKey("Content-Type")) {
        headers.remove("Content-Type");
    }

    headers.add("Content-Type", "application/xml");

    return response;
}

Then, you would need to add the interceptor to your RestTemplate object:

RestTemplate t = new RestTemplate();
t.getInterceptors().add(new MyXmlInterceptor());
Marvo
  • 17,845
  • 8
  • 50
  • 74
Thomas Ehardt
  • 155
  • 4
  • 11
0

Can you change the content-type header before unmarshalling happens, by adding a custom interceptor http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/http/client/ClientHttpRequestInterceptor.html ?

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • I tried changing the accept header with the interceptor to encourage them to use the correct header but that didn't work. – Nathanial Oct 23 '12 at 21:01
  • Sure, I would have tried that too. I mean fiddling the *response* header, after the response comes back. – artbristol Oct 23 '12 at 21:08
  • We solved the problem by adding an additional message converter to the restTemplate that accepts MediaType.TEXT_HTML. It's not ideal since it creates a redundant jaxb message converter, but it works--I tried adding the code in an answer, but it won't let me since I don't have the rep. – Nathanial Oct 23 '12 at 21:31
  • @Nathanial Sounds like a reasonable solution. Wait a while then post it as a solution. – artbristol Oct 23 '12 at 21:38