使用Jackson JSR310模块反序列化LocalDateTime [英] Deserializing LocalDateTime with Jackson JSR310 module

查看:2649
本文介绍了使用Jackson JSR310模块反序列化LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jackson数据类型JSR310页面描述的库,但我是仍然难以让它工作。

I'm using the library described the Jackson Datatype JSR310 page but I'm still having difficulty getting it to work.

我配置了以下bean:

I have configured the following bean:

@Bean
@Primary
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JSR310Module());
    return mapper;
}

当我调用我的REST API时,日期格式输出为 yyyy-MM-dd'T'HH:ss.SSSSSS ,例如 2015-04-11T00:10:3​​8.905847 。这可以通过我的AngularJS代码处理得很好。

When I call my REST API the date format output is yyyy-MM-dd'T'HH:ss.SSSSSS, e.g. 2015-04-11T00:10:38.905847. This gets handled by my AngularJS code just fine.

当我想向REST API提交内容时,日期发布为 yyyy-MM- dd'T'HH:mm:ss.SSS'Z',例如 2015-04-09T08:30:00.000Z

When I want to submit something to the REST API the date is posted as yyyy-MM-dd'T'HH:mm:ss.SSS'Z', e.g. 2015-04-09T08:30:00.000Z

杰克逊一直在抱怨'Z'。如果我查看文档中的 LocalDateTimeDeserializer ,它会使用 DateTimeFormatter.ISO_LOCAL_DATE_TIME ,它会归结为 ISO_LOCAL_DATE'T'ISO_LOCAL_TIME 并且它提到它没有覆盖区。

Jackson keeps complaining about the 'Z' at the end. If I look at the LocalDateTimeDeserializer in the documentation it uses the DateTimeFormatter.ISO_LOCAL_DATE_TIME which boils to ISO_LOCAL_DATE'T'ISO_LOCAL_TIME and it mentions it has no override zone.

所以我想我应该设置 DateFormat ObjectMapper 我正在创建:

So I figured I should set the DateFormat on the ObjectMapper I'm creating:

@Bean
@Primary
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JSR310Module());
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
    return mapper;
}

但这没有任何作用。我把它改成了像 yyyy-MM-dd 这样简单的东西,但是序列化的日期仍然保留了以前的格式,反序列化也没有受到影响。

But this does nothing. I changed it to something simple like yyyy-MM-dd but the serialized date remained in the previous format and the deserialization isn't affected either.

我在这做错了什么才能让它发挥作用?据我所知,我的JavaScript代码中的日期格式是ISO 8601格式......

What am I doing wrong here to get this to work? The date format in my JavaScript code is, as far as I know the ISO 8601 format...

推荐答案

没有必要编写自己的序列化程序。它足够使用默认的一个,但是用另一种格式( time_zone 一个)制作一个实例,这样就可以减少超出的部分:

It's not necessary to write your own serializer. It's enough use the default one, but making an instance with another format (the time_zone one) so that the exceeding part is just cut:

new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)

在我的情况下,我有一个 contextResolver 这样在配置级别实现:

In my case I've got a contextResolver like this to achieve at configuration level:

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

    public ObjectMapperContextResolver() {
        mapper = new ObjectMapper();
        JavaTimeModule javaTimeModule=new JavaTimeModule();
        // Hack time module to allow 'Z' at the end of string (i.e. javascript json's) 
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
        mapper.registerModule(javaTimeModule);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

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

这篇关于使用Jackson JSR310模块反序列化LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆