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<?>>();
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)