我正在使用Android resttemplate和MappingJacksonHttpMessageConverter。对于一些url,交换运作良好,但某些url会引发异常。Resttemplate实例化。
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
rootUrl = "http://someserver.com"
交换功能:
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();
}
问题 -
import org.codehaus.jackson.map.annotate.JsonRootName;
import java.util.LinkedList;
/**
* Wrapper for collection
*/
@JsonRootName(value = "questions")
public class Questions extends LinkedList<Question> {
}
问题
包含所有必要的注释,包括@JsonRootName
收到的Json内容类型为'application/json',一切都正确,并且最近运行良好。但是抛出了异常: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]