SpringBoot项目配置jackson对java8时间的序列化和反序列化

SpringBoot项目中配置使用java8 time

配置jackson对java8时间(LocalDateTime、LocalDate、LocalTime )的序列化和反序列化

1、相关配置如下

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
 * JacksonConfig
 */
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfig {
     
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer customizer() {
     
		return builder -> {
     
			/**
			 * 配置Date的格式化
			 */
			builder.locale(Locale.CHINA);
			builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
			builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
			/**
			 * 配置java8 时间配置
			 */
			builder.modules(new CustomJavaTimeModule());
		};
	}
}
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 配置java8 time的序列化器和反序列化器
 */
public class CustomJavaTimeModule extends SimpleModule {
     

	public CustomJavaTimeModule() {
     
		super(PackageVersion.VERSION);
		this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

		this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
	}
}

2、jackson在springboot项目中是通过JacksonAutoConfiguration自动配置的,我们需要在jackson自动配置之前把设置的java8的序列化器和反序列化器配置进去。
resources/META-INF下的spring.factories中配置JacksonConfig

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  package.JacksonConfig

小尾巴~~
只要有积累,就会有进步

你可能感兴趣的:(SpringBoot,jackson,java8,LocalDateTime,序列化,反序列化)