LMLPHP后院

Spring boot jackson datetime 配置技术

maybe yes 发表于 2017-11-08 10:52

Jackson 在遇到 datetime 类型的数据库字段会自动以时间戳数字的方式输出,该如何输出日期字符串呢?

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

使用的时候,发现有个 8 小时的时间差,解决方案在 application.properties 文件里面添加 spring.jackson.time-zone=GMT+8。 如果返回出来的时间数据需要固定的 String 格式,需要application.properties 添加如下配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss让该配置文件在启动的时候加载到 spring 容器中。

pojo 的 bean 里面通常会有 Date 类型的数据,直接通过 @ResponseBody 返回出去的是一个长整型时间戳。如果想要返回自定义的日期格式,需要做处理,网上有很多处理方式,大体都是继承、重写,比较复杂。实际上 JSON 已有注解 @JsonFormat 支持使用实例:@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")private Date createTime; 。

2024-04-26 21:59:24 1714139964 0.008020