spring boot中,不想每个实体类中都这样设置,有什么方法: @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;在Spring Boot中有几种方法可以避免在每个实体类中重复设置@JsonFormat注解:
方法一:全局配置(推荐)
在application.yml或application.properties中配置全局的日期格式:
YAML配置:
yaml
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 serialization: write-dates-as-timestamps: false
Properties配置:
properties
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false
方法二:自定义Jackson配置类
java
@Configuration public class JacksonConfig { @Bean @Primary public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { return builder -> { // 设置全局日期格式 builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss"); builder.timeZone(TimeZone.getTimeZone("GMT+8")); // 针对Java 8时间API的配置 builder.serializers(new LocalDateTimeSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); builder.deserializers(new LocalDateTimeDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); // 如果需要,也可以配置LocalDate等其他类型 builder.serializers(new LocalDateSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd"))); builder.deserializers(new LocalDateDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd"))); }; } }方法三:更详细的配置类
java
@Configuration public class JacksonConfig { @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // 设置Java 8时间模块 JavaTimeModule javaTimeModule = new JavaTimeModule(); // LocalDateTime序列化和反序列化 javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); // LocalDate序列化和反序列化 javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); objectMapper.registerModule(javaTimeModule); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); return objectMapper; } }方法四:使用Mixin(为特定类型统一配置)
java
@JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") abstract class LocalDateTimeMixin { } @Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer addCustomSerialization() { return builder -> { builder.mixIn(LocalDateTime.class, LocalDateTimeMixin.class); }; } }方法五:自定义注解(如果需要不同格式)
java
@Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") public @interface DefaultDateTimeFormat { } // 使用方式 @DefaultDateTimeFormat private LocalDateTime createTime;推荐方案
建议使用方法一(全局配置),最简单直接,能满足大部分场景。
如果项目中有多种不同的日期格式需求,可以:
使用方法一设置默认格式
对于特殊格式的字段,使用
@JsonFormat覆盖全局配置
java
// 全局使用 yyyy-MM-dd HH:mm:ss // 特殊字段使用其他格式 @JsonFormat(pattern = "yyyy/MM/dd") private LocalDate specialDate;
这样既能减少重复代码,又能保持灵活性。