Android RestTemplate,无法读取JSON:由于输入结束而没有要映射的内容

时间:2014-10-10 08:51:56

标签: android resttemplate

我发布了在服务器端创建资源的请求 服务器以201 Created状态代码和空内容回答。

这会在客户端产生以下错误:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map due to end-of-input
        at [Source: org.apache.http.conn.EofSensorInputStream@4354aff0; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
        at [Source: org.apache.http.conn.EofSensorInputStream@4354aff0; line: 1, column: 1]
        at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126)
        at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
        at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:317)
        at com.test.requests.favorites.PostFavoriteRequest.loadDataFromNetwork(PostFavoriteRequest.java:30)
        at com.test.requests.favorites.PostFavoriteRequest.loadDataFromNetwork(PostFavoriteRequest.java:10)
        at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
        at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:149)
        at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:216)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

如何处理空白内容的回复?

我已经尝试过以下作为响应类型类:

return getRestTemplate().postForObject(url, httpEntity, String.class);

return getRestTemplate().postForObject(url, httpEntity, Void.class);

return getRestTemplate().postForObject(url, httpEntity, null);

1 个答案:

答案 0 :(得分:0)

所以我会回答我自己的问题,因为这可能会对将来有所帮助。

使用RestTemplate.postForObject期望非空结果(不同于Void或null)并直接使用HttpMessageConverterExtractor
但是您可以使用使用ResponseEntityResponseExtractor的RestTemplate.exchange而不是直接使用上述方法执行的HttpMessageConverterExtractorResponseEntityResponseExtractor在创建响应提取器之前执行以下检查。

来自RestTemplate源代码:

private class ResponseEntityResponseExtractor<T> implements ResponseExtractor<ResponseEntity<T>> {

    private final HttpMessageConverterExtractor<T> delegate;

    public ResponseEntityResponseExtractor(Class<T> responseType) {
        if (responseType != null && !Void.class.equals(responseType)) {
            this.delegate = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
        } else {
            this.delegate = null;
        }
    }

    public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
        if (delegate != null) {
            T body = delegate.extractData(response);
            return new ResponseEntity<T>(body, response.getHeaders(), response.getStatusCode());
        }
        else {
            return new ResponseEntity<T>(response.getHeaders(), response.getStatusCode());
        }
    }
}