RestClientException: Failed to retrieve response. no suitable HttpMessageConverter

Using the curl command:

curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages 

I get a JSON response:

 {"data":[{"device":"18SE62","time":1494516023,"data":"3235","snr":"36.72",... 

I save the answer in a text file and parse it with Jackson and everything is fine

 ObjectMapper mapper = new ObjectMapper(); File f = new File(getClass().getResource ("/result.json").getFile()); MessageList messageList = mapper.readValue(f, MessageList.class); 

and I assume that I should get the same result using RestTemplate, but that’s not

 RestTemplate restTemplate = new RestTemplate(); MessageList messageList = restTemplate.getForObject("http://592693f43c87815f9b8145e9: f099c85d84d4e325a2186c02bd0caeef@backend.tdk.com /api/devicetypes/591570373c87894b4eece34d/messages", MessageList.class); 

Instead, I got an error

 Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) at com.tdk.controllers.restful.client.RestTemplateExample.main(RestTemplateExample.java:27) 

I tried setting contentType:

 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); MessageList messageList = restTemplate.getForObject(url, entity, MessageList.class); 

but then i got a compilation error

 The method getForObject(String, Class<T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity<String>, Class<MessageList>) 

I also tried to add Jackson's message converter

  List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); //Add the Jackson Message converter messageConverters.add(new MappingJackson2HttpMessageConverter()); //Add the message converters to the restTemplate restTemplate.setMessageConverters(messageConverters); MessageList messageList = restTemplate.getForObject(url, MessageList.class); 

But then I got this error:

 Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) at com.tdk.controllers.restful.client.RestTemplateExample.main(RestTemplateExample.java:51) 

I also tried to add a class

 @Configuration @EnableWebMvc public class MvcConf extends WebMvcConfigurationSupport { protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(converter()); addDefaultHttpMessageConverters(converters); } @Bean MappingJackson2HttpMessageConverter converter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); return converter; } } 

but I got an error:

 org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdk.domain.backend.MessageList] and content type [text/html;charset=iso-8859-1] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) 
+25
json spring rest spring-mvc resttemplate
source share
7 answers

The main problem here is the content type [text / html; charset = iso-8859-1] received from the service, however the actual content type must be application / json; charset = iso-8859-1

To overcome this, you can enter a special message converter. and register it for all kinds of responses (i.e. ignore the header of the response content type). Exactly

  List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); //Add the Jackson Message converter MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); // Note: here we are making this converter to process any kind of response, // not only application/*json, which is the default behaviour converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL)); messageConverters.add(converter); restTemplate.setMessageConverters(messageConverters); 
+57
source

I had a very similar problem and it turned out to be pretty simple; my client did not include Jackson's dependency, although the code compiled everything correctly, automatic magic converters for JSON were not included. Check out this RestTemplate related solution.

In short, I added Jackson's dependency to my pom.xml file, and it just worked:

 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> </dependency> 
+16
source

While the accepted answer resolved the original OP problem, I suspect that most people who find this question through a Google search are more likely (statistically speaking) to face a completely different problem that just leads to the same unsuitable HttpMessageConverter exception.

Undercover, what MappingJackson2HttpMessageConverter is that MappingJackson2HttpMessageConverter any exceptions that occur in its canRead() , which should automatically determine if the payload is suitable for json decoding. The exception is replaced by a simple logical return, which basically says: sorry, I do not know how to decode this message in a higher-level API ( RestClient ). Only after all other canRead () converter methods return false does the higher-level API throw an inappropriate HttpMessageConverter exception, completely hiding the true problem.

For people who haven't found the root cause (like you and me, but not OP), the way to fix this problem is to set onMappingJackson2HttpMessageConverter.canRead() stop the debugger in onMappingJackson2HttpMessageConverter.canRead() , and then enable the common onMappingJackson2HttpMessageConverter.canRead() break in any exception, and click Continue. The next exception is the true root cause.

My specific mistake was that one of the components referred to an interface that lacked proper deserialization annotations.

+6
source

If @Ilya Dyoshin's answer has not yet been received, try to get the answer in String Object.

(In my opinion, the error was resolved by the Ilya code fragment, the received response was unsuccessful (error) from the server.)

 HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); ResponseEntity<String> st = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); 

And translation in ResponseObject DTO (Json)

 Gson g = new Gson(); DTO dto = g.fromJson(st.getBody(), DTO.class); 
+3
source

In my case, @Ilya Dyoshin's solution did not work: the media type "*" was not allowed. I fix this error by adding a new converter to restTemplate this way during the initialization of MockRestServiceServer:

  MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); mappingJackson2HttpMessageConverter.setSupportedMediaTypes( Arrays.asList( MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM)); restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter); mockServer = MockRestServiceServer.createServer(restTemplate); 

(Based on a solution proposed by Yashwant Chawan on a blog named technicalkeeda)

Jn gerbaux

+2
source

In my case, this was caused by the lack of jaxon-core, jackson-annotations and jackson-databind jars from the path to the execution classes. He did not complain about the usual ClassNothFoundException, as might be expected, but about the error mentioned in the original question.

+1
source

You need to create your own converter and implement it before executing the GET request.

 RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL)); messageConverters.add(converter); restTemplate.setMessageConverters(messageConverters); 
0
source

All Articles