Reputation: 8395
I'm trying to use Spring for Android together with Jackson 2 to create a POJO from a rest call.
// Set the Accept header
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","vnd.livescore_app.api.v1+json")));
requestHeaders.setContentType(new MediaType("application","json;charset=utf-8"));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
restTemplate.getMessageConverters().add( new MappingJackson2HttpMessageConverter());
// Make the HTTP GET request, marshaling the response from JSON to an array of Events
ResponseEntity<AppVersionObject> responseEntity = restTemplate.exchange("https://example.com", HttpMethod.GET, requestEntity, AppVersionObject.class);
AppVersionObject object = responseEntity.getBody();
Crashes with the following log trace
05-13 10:55:42.656: E/AndroidRuntime(19401): FATAL EXCEPTION: Thread-1470
05-13 10:55:42.656: E/AndroidRuntime(19401): java.lang.IllegalArgumentException:Invalid token character ';' in token "json;charset=utf-8"
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.checkToken(MediaType.java:377)
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.<init>(MediaType.java:351)
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.<init>(MediaType.java:303)
05-13 10:55:42.656: E/AndroidRuntime(19401): at com.madinsweden.livescoretennis.service.CheckAppVersionTask.run(CheckAppVersionTask.java:41)
05-13 10:55:42.656: E/AndroidRuntime(19401): at java.lang.Thread.run(Thread.java:856)
I may of course change the content type to the more standardized application/json
but then I get the following error.
05-13 11:03:07.054: E/AndroidRuntime(20010): org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.madinsweden.livescoretennis.backend.AppVersionObject] and content type [application/vnd.livescore_app.api.v1+json;charset=utf-8]
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:79)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(Rest Template.java:641)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
05-13 11:03:07.054: E/AndroidRuntime(20010): at com.madinsweden.livescoretennis.service.CheckAppVersionTask.run(CheckAppVersionTask.java:51)
05-13 11:03:07.054: E/AndroidRuntime(20010): at java.lang.Thread.run(Thread.java:856)
Upvotes: 0
Views: 8098
Reputation: 80623
This line of code:
requestHeaders.setContentType(
new MediaType("application","json;charset=utf-8"));
Is incorrect, because that constructor is designed to take a type and subtype, and json;charset=utf-8
is not a valid subtype (rather, its a concatenation between a subtype and a type parameter). You want to be using the overloaded constructor that accepts a type, subtype, and parameter map:
final Map<String, String> parameterMap = new HashMap<String, String>(4);
parameterMap.put("charset", "utf-8");
requestHeaders.setContentType(
new MediaType("application","json", parameterMap));
This yields the correct 'Content-Type':
application/json;charset="utf-8"
Second problem is that you are specifying a custom response type (application/vnd.livescore_app.api.v1+json
), for which Spring does not have a HTTP message converter. You either have to write your own (as highlighted here), or change the response type to 'application/json'.
Upvotes: 5