SpringBoot中Jackson日期格式化技巧分享

网友投稿 510 2022-08-14


SpringBoot中Jackson日期格式化技巧分享

目录Jackson 日期格式化技巧后续问题补充:Jackson 统一配置 日期转换格式参考资料

Jackson 日期格式化技巧

使用 Spring Boot 时,需要使用 Jackson 处理一些 java Time API 类型的 jsON 序列化问题,在处理一些类的字段时,可以通过直接在属性上加注解的方式来指定其格式化样式。但是,昨天同事遇到一个格式化 Map 数据的问题,这样就不能通过加注解来解决格式化样式的问题了。

在网上各种搜索,各种尝试后,终于解决了这个问题,记录一下,以备不时之需。

闲言少叙,直接上代码:

package com.diguage.demo.config;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.util.StdDateFormat;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.time.LocalDate;

import java.time.LocalDateTime;

import static com.fasterxml.jLTcoVyackson.databind.SerializationFeature.*;

import static java.time.format.DateTimeFormatter.ofPattern;

/

* 配置类

*

* @author D瓜哥 https://diguage.com

*/

@Configuration

public class Config {

/

* 创建 ObjectMapper 对象,配置日期格式化

*

* @author D瓜哥 https://diguage.com

*/

@Bean

@Primary

public ObjectMapper objectMapper() {

ObjectMapper mapper = new ObjectMapper();

String dateTimepattern = "yyyy-MM-dd HH:mm:ss";

String datePattern = "yyyy-MM-dd";

DateFormat dateFormat = new SimpleDateFormat(dateTimepattern);

mapper.setDateFormat(dateFormat);

mapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);

mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));

JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addDeserializer(LocalDate.class,

new LocalDateDeserializer(ofPattern(datePattern)));

javaTimeModule.addSerializer(LocalDate.class,

new LocalDateSerializer(ofPattern(datePattern)));

javaTimeModule.addDeserializer(LocalDateTime.class,

new LocalDateTimeDeserializer(ofPattern(dateTimepattern)));

javaTimeModule.addSerializer(LocalDateTime.class,

new LocalDateTimeSerializer(ofPattern(dateTimepattern)));

mapper.registerModule(javaTimeModule);

return mapper;

}

}

后续问题

不知道通过这种方式指定日期格式化样式后,在处理一些打格式化样式注解的字段时,会有什么样的表现?有机会测试一下。

补充:Jackson 统一配置 日期转换格式

方式一:配置文件yml中配置

spring:

jackson:

default-property-inclusion: ALWAYS

time-zone: GMT+8

date-format: yyyy-MM-dd HH:mm:ss

这样序列化后,Date类型会被格式化成配置中的格式。

方式二:配置类中配置创建JacksonConfig.java

@Configuration

public class JacksonConfig {

@Bean

@Order(Ordered.HIGHEST_PRECEDENCE)

public Jackson2ObjectMapperBuilderCustomizer customJackson() {

return new Jackson2ObjectMapperBuilderCustomizer() {

@Override

public void customize(Jackson2ObjectMapperBuilder builder) {

LTcoVybuilder.serializerByType(LocalDateTime.class,

new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

builder.serializerByType(LocalDate.class,

new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

builder.serializerByType(LocalTime.class,

new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));http://

builder.deserializerByType(LocalDateTime.class,

new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

builder.deserializerByType(LocalDate.class,

new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

builder.deserializerByType(LocalTime.class,

new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

builder.serializationInclusion(JsonInclude.Include.NON_NULL);

builder.failOnUnknownProperties(false);

builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

}

};

}

}

参考资料

Jackson Datejson - Java 8 LocalDate Jackson format


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring
下一篇:Java实现企业微信消息推送功能的详细步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~