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
?