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

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

问题描述

我正在使用 Jackson Datatype 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:38.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

Jackson 一直在抱怨结尾的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.

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

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天全站免登陆