spring boot3 自定义 starter
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
自动装配时间序列化
@AutoConfiguration
@ConditionalOnBean(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer(){
return builder -> {
builder.locale(Locale.CHINA);
builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
builder.modules(new GenericJavaTimeModule());
};
}
public static class GenericJavaTimeModule extends SimpleModule{
public GenericJavaTimeModule() {
super(PackageVersion.VERSION);
this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER));
this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
this.addSerializer(Instant.class, InstantSerializer.INSTANCE);
this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER));
this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
this.addDeserializer(Instant.class, InstantDeserializer.INSTANT);
}
}
}
配置文件
- @AutoConfiguration 是spring boot2.7新引入的,自动配置类必须放进下面的文件里才算自动配置类
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.core.config.WebMvcDateConfiguration
com.core.config.JacksonConfiguration