【问题标题】:spring RestTemplate no Content-Typespring RestTemplate 没有 Content-Type
【发布时间】:2012-01-21 19:48:33
【问题描述】:

我使用spring-android连接服务器获取json并映射到对象类:

罐子:

jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
spring-android-auth-1.0.0.M4.jar
spring-android-core-1.0.0.M4.jar
spring-android-rest-template-1.0.0.M4.jar

来源:

RestTemplate restTemplate = new RestTemplate(/* clientHttpRequestFactory */);

ClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
restTemplate.setRequestFactory(clientHttpRequestFactory);

MappingJacksonHttpMessageConverter httpMessageConverter = new MappingJacksonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(new MediaType("application", "json"));
httpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);

List<HttpMessageConverter<?>> httpMessageConverters = restTemplate
                .getMessageConverters();
httpMessageConverters.add(httpMessageConverter);
restTemplate.setMessageConverters(httpMessageConverters);

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("param1", "param1");
map.add("param2", "2");

T t = restTemplate.postForObject(url, map, T.class);

来自服务器的结果是一个 json 字符串,如:

{"aaa":"111", "bbb":"222"}

结果是:

Caused by: org.springframework.web.client.RestClientException: Cannot extract response: no Content-Type found
     at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:60)
     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:470)
     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:425)
     at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:303)

响应标头中不能有Content-Type。如何让RestTemplate 不关心Content-Type

【问题讨论】:

    标签: android spring jackson


    【解决方案1】:

    这看起来像是继承自 Spring 3.0.x 代码的缺陷。有关详细信息,请参阅https://jira.springsource.org/browse/SPR-7911。您可以尝试here

    列出的解决方法之一

    【讨论】:

    • 该问题与 204 No Content 无关。他说服务器返回一个响应正文,但没有 Content-Type 标头。
    猜你喜欢
    • 2011-04-24
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2019-08-05
    • 2019-04-15
    • 2021-03-25
    • 2013-06-09
    • 2020-10-27
    相关资源
    最近更新 更多