Springboot Jackson配置根本方案, 日期格式化, 时区设置生效
当项目集成配置的功能越来越多, 说不准哪个配置就影响到了什么.
比如你启用了EnableMvC, 默认配置文件配置的一些文件就失效了. 虽然约定大于配置,让springboot可以极简化构建, 但不熟悉内部各个组件执行原理会导致我们经常出一些莫名其妙的问题, 比如配置不生效,比如Jackson的日期格式化.
debug了很久, 配置文件不生效, 直接声明ObjectMapper也不管用. 原因就在于Springboot所谓的简化是通过一系列的条件配置产生, 比如WebMvcConfigurationSupport, 里面到处都是if-else配置逻辑.
这些配置开关复杂且并不知道散落在哪里.
既然如此, 我直接手动配置好了. 关于springboot json序列化的关键是MappingJackson2HttpMessageConverter, 我们需要把springboot默认给配置的converter干掉, 然后放上自己的.
@Configuration
public class RequestHandlerConfig extends WebMvcConfigurationSupport {
private Logger logger = LoggerFactory.getLogger(RequestHandlerConfig.class);
@Override
public void addInterceptors(InterceptorRegistry registry) {
//请求上下文初始化拦截器配置
logger.info("初始化拦截器完成.....");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public ObjectMapper jacksonObjectMapperCustomization() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
format.setTimeZone(timeZone);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.timeZone(timeZone)
.dateFormat(format);
return builder.build();
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapperCustomization()));
}
}
让LocalDateTime格式化:
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Ryan Miao 01399596
* @date 2021/1/28 16:35
*/
@Configuration
public class DateConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer enumCustomizer() {
return builder -> builder.deserializerByType(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
.deserializerByType(LocalDate.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
.serializerByType(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
.serializerByType(LocalDate.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
;
}
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(LocalDateTime.now());
System.out.println(format);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器