0

我正在尝试将 Spring for Android 与 Jackson 2 一起使用,从休息呼叫中创建 POJO。

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

崩溃并出现以下日志跟踪

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)

我当然可以将内容类型更改为更标准化application/json,但随后出现以下错误。

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

1 回答 1

5

这行代码:

requestHeaders.setContentType(
    new MediaType("application","json;charset=utf-8"));

不正确,因为该构造函数被设计为采用typesubtype,并且json;charset=utf-8不是有效的子类型(相反,它是子类型和类型参数之间的串联)。您希望使用接受类型子类型参数映射的重载构造函数:

final Map<String, String> parameterMap = new HashMap<String, String>(4);
parameterMap.put("charset", "utf-8");
requestHeaders.setContentType(
    new MediaType("application","json", parameterMap));

这会产生正确的“内容类型”:

application/json;charset="utf-8"

第二个问题是您正在指定自定义响应类型 ( application/vnd.livescore_app.api.v1+json),Spring 没有针对该类型的 HTTP 消息转换器。您要么必须自己编写(如此突出显示),要么将响应类型更改为“application/json”。

于 2013-05-14T03:35:27.013 回答