我使用Androd resttemplate和MappingJacksonHttpMessageConverter。 對於一些網址交換效果不錯,但一個是造成異常。 實例化ResttemplateResttemplate android:RestClientException:無法提取響應:沒有合適的HttpMessageConverter發現
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> {
}
Question
擁有所有必要的註釋,包括@JsonRootName
正在添加的Json有內容ŧ ype'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]