Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no suitable HttpMessageConverter found for response type

Using spring, with this code :

List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters(); for(HttpMessageConverter httpMessageConverter : messageConverters){   System.out.println(httpMessageConverter); } ResponseEntity<ProductList> productList = restTemplate.getForEntity(productDataUrl,ProductList.class); 

I get

org.springframework.http.converter.ByteArrayHttpMessageConverter@34649ee4 org.springframework.http.converter.StringHttpMessageConverter@39fba59b org.springframework.http.converter.ResourceHttpMessageConverter@383580da org.springframework.http.converter.xml.SourceHttpMessageConverter@409e850a org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@673074aa org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e3b79d3 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@52bb1b26  org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycopmany.ProductList] and content type [text/html;charset=UTF-8] 

The a snippet of the pojo :

@XmlRootElement(name="TheProductList") public class ProductList {  @XmlElement(required = true, name = "date") private LocalDate importDate; 
like image 900
NimChimpsky Avatar asked Feb 18 '14 12:02

NimChimpsky


2 Answers

From a Spring point of view, none of the HttpMessageConverter instances registered with the RestTemplate can convert text/html content to a ProductList object. The method of interest is HttpMessageConverter#canRead(Class, MediaType). The implementation for all of the above returns false, including Jaxb2RootElementHttpMessageConverter.

Since no HttpMessageConverter can read your HTTP response, processing fails with an exception.

If you can control the server response, modify it to set the Content-type to application/xml, text/xml, or something matching application/*+xml.

If you don't control the server response, you'll need to write and register your own HttpMessageConverter (which can extend the Spring classes, see AbstractXmlHttpMessageConverter and its sub classes) that can read and convert text/html.

like image 89
Sotirios Delimanolis Avatar answered Oct 10 '22 01:10

Sotirios Delimanolis


You could also simply tell your RestTemplate to accept all media types:

@Bean public RestTemplate restTemplate() {    final RestTemplate restTemplate = new RestTemplate();     List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));    messageConverters.add(converter);    restTemplate.setMessageConverters(messageConverters);     return restTemplate; } 
like image 32
Markoorn Avatar answered Oct 10 '22 01:10

Markoorn