2017-06-16 69 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