RestTemplate を使用していて、オブジェクトのデシリアライズに問題があります。これが私がやっていることです。JSON 応答は次のようになります。
{
"response": {
"Time": "Wed 2013.01.23 at 03:35:25 PM UTC",
"Total_Input_Records": 5,
},-
"message": "Succeeded",
"code": "200"
}
jsonschema2pojoを使用して、この Json ペイロードを POJO に変換しました
public class MyClass {
@JsonProperty("response")
private Response response;
@JsonProperty("message")
private Object message;
@JsonProperty("code")
private Object code;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//bunch of getters and setters here
}
public class Response {
@JsonProperty("Time")
private Date Time;
@JsonProperty("Total_Input_Records")
private Object Total_Input_Records;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//bunch of getters and setters here
}
これは、例外が発生しているリクエスト処理です。
String url = "http://example.com/someparams";
RestTemplate template = new RestTemplate(
new HttpComponentsClientHttpRequestFactory());
FormHttpMessageConverter converter = new FormHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(new MediaType("application", "x-www-form-urlencoded"));
converter.setSupportedMediaTypes(mediaTypes);
template.getMessageConverters().add(converter);
MyClass upload = template.postForObject(url, null, MyClass.class);
これがイライラする部分です、例外です(意図的にトリミングされ、完全ではありません)。不足しているものはありますか?
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "Time" (Class com.temp.pointtests.Response), not marked as ignorable
at [Source: org.apache.http.conn.EofSensorInputStream@340ae1cf; line: 1, column: 22 (through reference chain: com.temp.pointtests.MyClass["response"]->com.temp.pointtests.Response["Time"]);]
+++++解決済みの更新++++++
Spring が Jackson 2 を使用する MappingJackson2HttpMessageConverter を追加したのを見ました。上記のコードの MappingJacksonHttpMessageConverter は Jackson Pre2.0 バージョンを使用しており、機能しないためです。ただし、Jackson 2.0 では機能します。MappingJackson2HttpMessageConverter が利用可能になったので、それを RestTemplate に追加できるようになり、すべて正常に動作します :-)。同じ問題を抱えている人のためのコードは次のとおりです。
String url = "http://example.com/someparams";
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity request = new HttpEntity(headers);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter map = new MappingJackson2HttpMessageConverter();
messageConverters.add(map);
messageConverters.add(new FormHttpMessageConverter());
template.setMessageConverters(messageConverters);
MyClass msg = template.postForObject(url, request, MyClass.class);