2017-06-16 51 views
1

我有JSON這樣的:Fasterxml傑克遜DateTimeSerializer使得沒有合適的HttpMessageConverter

{ 
    "data": { 
     "name": "Rudi Wijaya", 
     "photoId": "rudi_wijaya_facebook_1763547129_fb_8673568292060043679", 
     "id": "rudi", 
     "creationTime": 1495187730806, 
     "modificationTime": 1497599396889, 
     "description": null, 
     : 
     : 
    }, 
    "message": null, 
    "httpStatus": "OK" 
} 

,我把它從靜止模板春天的android:

restTemplate = new RestTemplate();  
final ObjectMapper objectMapper = new ObjectMapper(); 
objectMapper.registerModule(new JodaModule());  
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
converter.setObjectMapper(objectMapper); 
restTemplate.getMessageConverters().add(converter);  
requestHeaders = new HttpHeaders();  
requestHeaders.setContentType(MediaType.APPLICATION_JSON); 

final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(context.getString(R.string.api_log_in)); 
uriBuilder.queryParam("username",upPerson.getId()); 
uriBuilder.queryParam("password",upPerson.getPassword()); 
final URI uri = uriBuilder.build().toUri(); 
final HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders); 

final ResponseEntity<CatalogResponse<Person>> responseEntity = restTemplate.exchange(
     uri, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<CatalogResponse<Person>>() {}); 
final CatalogResponse<Person> result = responseEntity.getBody(); 
final Person loggedInPerson = result.getData(); 

POJO的:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Person { 

    private String name; 

    private String photoId; 

    private String id; 

    @JsonSerialize(using = DateTimeSerializer.class) 
    @JsonDeserialize(using = DateTimeDeserializer.class) 
    private DateTime creationTime; 

    @JsonSerialize(using= DateTimeSerializer.class) 
    @JsonDeserialize(using = DateTimeDeserializer.class) 
    private DateTime modificationTime; 

    private String slug;  

    @JsonSerialize(using= LocalDateSerializer.class) 
    @JsonDeserialize(using=LocalDateDeserializer.class) 
    private LocalDate birthDate; 


    public Person() { 
     super(); 
    } 

    : 
    (set and getter) 
    : 

} 

public class CatalogResponse<T> implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Nullable 
    private T data; 
    private String message; 
    private HttpStatus httpStatus; 
    private Exception e; 

    public CatalogResponse() { 
     super(); 
    } 

    : 
    (set and getter) 
    : 

} 

而且我得到錯誤:

Failed to sign-in use username or email'rudi': org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [id.co.bippo.tlc.controller.CatalogResponse<id.co.bippo.tlc.controller.entities.Person>] and content type [application/json;charset=UTF-8] 
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [id.co.bippo.tlc.controller.CatalogResponse<id.co.bippo.tlc.controller.entities.Person>] and content type [application/json;charset=UTF-8] 
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102) 
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:738) 
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:723) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:544) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512) 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:490) 
環境

compile group: 'joda-time', name: 'joda-time', version: '2.9.9' 

compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3' 

compile (
      [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.9'], 
      [group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.9'], 
      [group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.9'] 
    ) 
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-joda 
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-joda', version: '2.8.9' 

<jackson.version>2.8.9</jackson.version> 
<joda-time.version>2.9.9</joda-time.version> 

如果我刪除了所有:

@JsonDeserialize(using = DateTimeDeserializer.class) 

錯誤消息消失...

+1

@Abhi謝謝編輯json ..我無法格式化它,因爲我得到了「它看起來像你的帖子主要是代碼,請添加更多的細節。「 –

回答

0

@JsonDeserialize指定解串器必須有一個無參數的構造傑克遜能夠使用它。正如你在DateTimeDeserializer docs中看到的那樣,它沒有一個沒有參數的構造函數。

使用它的預期方式是沒有@JsonDeserialize註釋。只要你註冊JodaModule(你這樣做)它將在內部使用DateTimeDeserializer每當它必須反序列化爲DateTime