4

I used spring-android to connect to a server to get json and map it to an object class:

jars:

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

source:

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);

The result from the server is a json string like:

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

The result is:

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)

There must be no Content-Type in the response headers. How can I make RestTemplate not care about Content-Type?

Alex Wittig
  • 2,800
  • 1
  • 33
  • 42
YETI
  • 928
  • 3
  • 12
  • 24

1 Answers1

-2

This looks like a deficiency inherited from the Spring 3.0.x code. See https://jira.springsource.org/browse/SPR-7911 for details. You could try one of the workarounds listed here

Community
  • 1
  • 1
gnuf
  • 2,722
  • 1
  • 25
  • 32
  • 2
    The problem is not related to a 204 No Content. He says the server returns a response body but without a `Content-Type` header. – Alex Wittig Jan 07 '15 at 22:51