简体   繁体   English

Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter

[英]Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type

I'm trying to retrieve some data from a REST service using spring for Android. 我正在尝试使用Spring for Android从REST服务检索一些数据。 However I'm running into problems. 但是我遇到了问题。 I'm also using Robospice - and as such have a method like so: 我也在使用Robospice-因此具有这样的方法:

 public FunkyThingsList loadDataFromNetwork() throws Exception {
        String baseURI =context.getResources().getString(R.string.baseWebServiceURI);
                String uri = baseURI + "/FunkyThings/date/" + dateToLookUp.toString("yyyy-MM-dd");
        RestTemplate restTemplate = getRestTemplate();
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setAccept( Arrays.asList(MediaType.APPLICATION_JSON));
        HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
        headers.setAuthorization(authHeader);

        // Create the request body as a MultiValueMap
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter();
        FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
        List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
        msgConverters.add(formConverter);
        msgConverters.add(stringConverter);
        restTemplate.setMessageConverters(msgConverters);

        HttpEntity<?> httpEntity = new HttpEntity<Object>(map, headers);
        final ResponseEntity<FunkyThingsList> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity,FunkyThingsList.class);

        return responseEntity.getBody();
    }

Unfortunately this isn't working. 不幸的是,这不起作用。 I'm getting the following exception thrown: 我收到以下异常:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.MyProject.DataClassPojos.RoboSpiceArrayLists.FunkyThingsList] and content type [application/json;charset=utf-8]

Now based on my Googling I get the feeling I need to add a message converter. 现在基于我的谷歌搜索,我感觉我需要添加一个消息转换器。 I'm just not sure which message converter I need, or where I add it? 我只是不确定我需要哪个消息转换器,或在哪里添加它?

For the default JSON HttpMessageConverter , you'll need to add either Jackson 1 or Jackson 2 to your classpath. 对于默认的JSON HttpMessageConverter ,您需要将Jackson 1或Jackson 2添加到您的类路径中。

Otherwise, you can add some other JSON library and write your own HttpMessageConverter which can do the deserialization. 否则,您可以添加其他一些JSON库并编写自己的HttpMessageConverter来进行反序列化。 You add it to the RestTemplate . 您将其添加到RestTemplate You can either use the constructor or this method . 您可以使用构造函数或此方法

If you are following the Spring for Android tutorial and you get the same error - this might be of help: 如果您遵循的是Spring for Android教程,并且遇到相同的错误-这可能会有所帮助:

In your MainActivity.java: 在您的MainActivity.java中:

...
RestTemplate restTemplate = new RestTemplate();
MappingJacksonHttpMessageConverter converter = new
MappingJacksonHttpMessageConverter();    
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_HTML));
restTemplate.getMessageConverters().add(converter);

Use the converter in the call and configure it to use as a Media Type the TEXT_HTML, then use the instance of the converter. 在调用中使用转换器并将其配置为用作媒体类型TEXT_HTML,然后使用转换器的实例。

Also the tutorial is a bit outdated so use the latest versions as suggested here in your build.gradle: 另外,该教程有些过时,因此请使用build.gradle中建议的最新版本:

dependencies{
 //Spring Framework for REST calls and Jackson for JSON processing
 compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'

 compile 'com.fasterxml.jackson.core:jackson-databind:2.4.1.3'
}
repositories {
 maven {
    url 'https://repo.spring.io/libs-milestone'
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type Spring Rest客户端实现:无法提取响应:没有为xstreamMarshaller找到适合的响应类型的HttpMessageConverter - Spring Rest Client implementation : Could not extract response: no suitable HttpMessageConverter found for response type with xstreamMarshaller 无法提取响应:找不到合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found 无法提取响应:没有找到适合响应类型 class 和内容类型 [text/html] 的 HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html] 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 休息模板:无法提取响应:当我们得到 xml 响应时,没有找到适合响应类型的 HttpMessageConverter,未绑定到 JAVA 对象 - Rest Template:Could not extract response: no suitable HttpMessageConverter found for response type when we got xml response, not bind to JAVA object 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type 没有为响应类型找到合适的HttpMessageConverter - Custom RestTemplate - No suitable HttpMessageConverter found for response type - Custom RestTemplate 无法提取响应:jaxb2marshaller没有合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter with jaxb2marshaller 找不到适用于响应类型[..]和内容类型[application / json]的HttpMessageConverter - No suitable HttpMessageConverter found for response type [..] and content type [application/json]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM