首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Resttemplate android: RestClientException:无法提取响应:找不到合适的HttpMessageConverter

Resttemplate android: RestClientException:无法提取响应:找不到合适的HttpMessageConverter
EN

Stack Overflow用户
提问于 2013-01-23 18:06:41
回答 1查看 7.1K关注 0票数 3

我使用Androd resttemplate和MappingJacksonHttpMessageConverter。对于某些urls,交换工作得很好,但其中一个会导致异常。Resttemplate的实例化

代码语言:javascript
运行
复制
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
rootUrl = "http://someserver.com"

和交换功能:

代码语言:javascript
运行
复制
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();
}

问题-

代码语言:javascript
运行
复制
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通信有内容类型'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]

EN

回答 1

Stack Overflow用户

发布于 2013-02-18 10:49:51

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

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14477010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档