3

I'm using Androd resttemplate and MappingJacksonHttpMessageConverter. For some urls exchange works well, but one is causing exception. Instantiation of Resttemplate

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

and 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 having all needed annotations including @JsonRootName

Json comming have content type 'application/json' and all is right and recently works well. But 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]

4

1 回答 1

1

我看不出你的代码有什么问题。Spring 3.2.x 添加了使用 Jackson 2- 的转换器MappingJackson2HttpMessageConverter。也许你可以试试这个转换器。这解决了我过去遇到的类似问题。确保您的 POJO 具有 Jackson 2 注释。希望对您有所帮助。

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);
于 2013-02-18T02:49:51.387 回答