iis搭建网站404浙江网站建设前十的公司
2026/4/18 12:27:47 网站建设 项目流程
iis搭建网站404,浙江网站建设前十的公司,腾讯广告联盟官网,crm系统视频spring boot中#xff0c;不想每个实体类中都这样设置#xff0c;有什么方法#xff1a;JsonFormat(pattern yyyy-MM-dd HH:mm:ss)private LocalDateTime createTime;在Spring Boot中有几种方法可以避免在每个实体类中重复设置JsonFormat注解#xff1a;方法一…spring boot中不想每个实体类中都这样设置有什么方法 JsonFormat(pattern yyyy-MM-dd HH:mm:ss) private LocalDateTime createTime;在Spring Boot中有几种方法可以避免在每个实体类中重复设置JsonFormat注解方法一全局配置推荐在application.yml或application.properties中配置全局的日期格式YAML配置yamlspring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT8 serialization: write-dates-as-timestamps: falseProperties配置propertiesspring.jackson.date-formatyyyy-MM-dd HH:mm:ss spring.jackson.time-zoneGMT8 spring.jackson.serialization.write-dates-as-timestampsfalse方法二自定义Jackson配置类javaConfiguration public class JacksonConfig { Bean Primary public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { return builder - { // 设置全局日期格式 builder.simpleDateFormat(yyyy-MM-dd HH:mm:ss); builder.timeZone(TimeZone.getTimeZone(GMT8)); // 针对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))); }; } }方法三更详细的配置类javaConfiguration 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(GMT8)); return objectMapper; } }方法四使用Mixin为特定类型统一配置javaJsonDeserialize(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); }; } }方法五自定义注解如果需要不同格式javaRetention(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;这样既能减少重复代码又能保持灵活性。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询