2016-01-20 98 views
1

我使用Jersey作爲Jackson作爲JSON提供程序。我能夠將ZonedDateTime序列化爲JSON,但是當我想要反序列化時,它會給我錯誤,如下所示。無法使用Jackson反序列化ZonedDateTime使用Jackson

你能幫我告訴確切的配置需要得到這個反序列化工作。

所致:com.fasterxml.jackson.databind.JsonMappingException:00::不能從字符串值('2016-01-21T21實例化類型[簡單類型,類java.time.ZonedDateTime]的值00Z 「);無單String構造/工廠方法

我的映射配置如下:

@Provider 
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> { 
    private final ObjectMapper MAPPER; 

    public ObjectMapperContextResolver() { 
     MAPPER = new ObjectMapper(); 
     //This would add JSR310 (Datetime) support while converting date to JSON using JAXRS service 
     MAPPER.registerModule(new JavaTimeModule()); 
     //Below line would disable use of timestamps (numbers), 
     //and instead use a [ISO-8601 ]-compliant notation, which gets output as something like: "1970-01-01T00:00:00.000+0000". 
     MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 
    } 

    @Override 
    public ObjectMapper getContext(Class<?> type) { 
     return MAPPER; 
    } 
} 

回答

1

我發現這個問題,其實問題不是使用標準傑克遜提供反序列化。就我而言,我使用Jersey客戶端來獲取JSON,然後使用readEntity方法進行反序列化。

問題是平臺客戶端不知道jsr310模塊,所以通過註冊已添加jsr310的contextresolver解決了問題。簡而言之,如果使用正常的傑克遜提供程序,則不需要爲ZonedDateTime進行分散和反序列化操作。

下面是我參考的參考代碼,以獲得更好的清晰度。

public class RESTClientImpl{ 

    /* 
    * ***This is very important, JacksonJsonProvider is the implementation of 
    * MessageBodyWriter/Reader which is required for "readEntity" method, 
    * else it would throw MessageBodyWriter/Reader not found exception 
    * 
    * https://jersey.java.net/documentation/latest/message-body-workers.html#mbw.ex.client.mbr.reg 
    * 
    * Registering of ObjectMapperContextResolver is important as we have registered JSR310 module there and without registering this, 
    * Jersey client is not aware of JSR310 module, so it will not be able to de-serialize ZonedDateTime 
    */ 
    private final Client client = ClientBuilder.newClient(new ClientConfig().register(LoggingFilter.class)).register(JacksonJsonProvider.class) 
      .register(ObjectMapperContextResolver.class); 

    public User get(URI uri) { 
     WebTarget webTarget = client.target(uri); 

     Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); 
     Response response = invocationBuilder.get();  
     User user = response.readEntity(User.class); 
     return user; 
    } 
} 


@Provider 
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> { 
    private final ObjectMapper MAPPER; 

    public ObjectMapperContextResolver() { 
     MAPPER = new ObjectMapper(); 
     //This would add JSR310 (Datetime) support while converting date to JSON using JAXRS service 
     MAPPER.registerModule(new JavaTimeModule()); 
     //Below line would disable use of timestamps (numbers), 
     //and instead use a [ISO-8601 ]-compliant notation, which gets output as something like: "1970-01-01T00:00:00.000+0000". 
     MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 

     SimpleModule simpleModule = new SimpleModule(); 
     simpleModule.addDeserializer(Object.class, new ZonedDateTimeDeserializer()); 
     MAPPER.registerModule(simpleModule); 
    } 

    @Override 
    public ObjectMapper getContext(Class<?> type) { 
     return MAPPER; 
    } 
} 
相關問題