import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; 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.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.time.format.DateTimeFormatter; @Configuration public class JacksonConfiguration { @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { // formatter DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // deserializers builder.deserializers(new LocalDateDeserializer(dateFormatter)); builder.deserializers(new LocalDateTimeDeserializer(dateTimeFormatter)); // serializers builder.serializers(new LocalDateSerializer(dateFormatter)); builder.serializers(new LocalDateTimeSerializer(dateTimeFormatter)); builder.serializerByType(Long.TYPE, ToStringSerializer.instance); builder.serializerByType(Long.class, ToStringSerializer.instance); }; } }
spring: jackson: date-format: "yyyy-MM-dd HH:mm:ss" time-zone: "GMT+8"
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
标签:jackson,Springboot,builder,DateTimeFormatter,LocalDateTime,import,com,格式化,faster From: https://www.cnblogs.com/namedlxd/p/18408160