非Spring boot下,ObjectMapper配置LocaleDateTime序列化和反序列化

目录

1,非Spring boot环境下,直接使用ObjectMapper序列化LocaleDateTime会报错

2,ObjectMapper使用findAndRegisterModules(),序列化正常,序列化后日期格式为非常用格式

3,Mapper设置DateFormat后,日期格式改变为ISO模式,与设置的Pattern不匹配

4,使用JavaTimeModule设置LocaleDateTime序列器并将其注册到ObjectMapper中,序列化后日期格式满足设定值


1,非Spring boot环境下,直接使用ObjectMapper序列化LocaleDateTime会报错

在非Spring boot情况下,没有使用自动配置,使用ObjectMapper序列化LocaleDateTime对象时会报错

public void systemMillisTest() throws Exception {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(1676104662050L), ZoneId.systemDefault());
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("过期时间:" + objectMapper.writeValueAsString(time));
}

运行后报错,提示需要添加jackson-datatype-jsr310,实际上项目资源有导入这个包

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
 

2,ObjectMapper使用findAndRegisterModules(),序列化正常,序列化后日期格式为非常用格式
public void systemMillisTest() throws Exception {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(1676104662050L), ZoneId.systemDefault());
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        System.out.println("过期时间:" + objectMapper.writeValueAsString(time));
}

过期时间:[2023,2,11,16,37,42,50000000]

Process finished with exit code 0

3,Mapper设置DateFormat后,日期格式改变为ISO模式,与设置的Pattern不匹配
public void systemMillisTest() throws Exception {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(1676104662050L), ZoneId.systemDefault());
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(dateFormat);
        System.out.println("过期时间:" + objectMapper.writeValueAsString(time));
}

过期时间:"2023-02-11T16:37:42.05"

Process finished with exit code 0

4,使用JavaTimeModule设置LocaleDateTime序列器并将其注册到ObjectMapper中,序列化后日期格式满足设定值
public void systemMillisTest() throws Exception {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(1676104662050L), ZoneId.systemDefault());
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule timeModule = new JavaTimeModule();
        timeModule.addDeserializer(LocalDateTime.class, new 
        LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        timeModule.addSerializer(LocalDateTime.class, new 
        LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        objectMapper.registerModule(timeModule);
       System.out.println("过期时间:" + objectMapper.writeValueAsString(time));
}

过期时间:"2023-02-11 16:37:42"

Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值