Restremplate android: RestClientException: Failed to retrieve response: No matching HttpMessageConverter found

I am using Androd resttemplate and MappingJacksonHttpMessageConverter. For some exchanges, the URL works well, but one throws an exception. Resttemplate execution

restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
rootUrl = "http://someserver.com"

      

and the exchange function:

public Questions loadQuestQuestions(int quest_id, String token) {
    HashMap<String, Object> urlVariables = new HashMap<String, Object>();
    urlVariables.put("quest_id", quest_id);
    urlVariables.put("token", token);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType("application/json")));
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(httpHeaders);
    return restTemplate.exchange(rootUrl.concat("/tasks/{quest_id}/questions?token={token}"), HttpMethod.GET, requestEntity, Questions.class, urlVariables).getBody();
}

      

Questions -

import org.codehaus.jackson.map.annotate.JsonRootName;
import java.util.LinkedList;

/**
 * Wrapper for collection
*/
@JsonRootName(value = "questions")
public class Questions extends LinkedList<Question> {
}

      

Question

with all the necessary annotations including @JsonRootName

Json comming has content type 'application / json' and everything is correct and has been working well lately. But the exception is thrown: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [Questions] and content type [application/json;charset=utf-8]

+3


source to share


1 answer


I don't see anything wrong with your code. Spring 3.2.x added converters that use Jackson 2- MappingJackson2HttpMessageConverter

. Maybe you can try this converter. This solved similar problems that I had in the past. Make sure your POJOs have Jackson 2 annotations. Hope this helps you.



RestTemplate template = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter map = new MappingJackson2HttpMessageConverter();
messageConverters.add(map);
template.setMessageConverters(messageConverters);
MyClass msg = template.postForObject(url, request, MyClass.class);

      

+1


source







All Articles