Fasterxml Jackson DateTimeSerializer没有合适的HttpMessageConverter

时间:2017-06-16 08:23:50

标签: android jackson

我有json喜欢:

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

我从rest-template spring 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 个答案:

答案 0 :(得分:0)

@JsonDeserialize中指定的反序列化器必须具有一个no-args构造函数,以便Jackson能够使用它。正如您在DateTimeDeserializer docs中看到的那样,它没有没有args构造函数。

使用它的预期方法是没有@JsonDeserialize注释。只要您注册JodaModule(您这样做),只要必须反序列化为DateTimeDeserializer

,它就会在内部使用DateTime