springboot3.0 配置全局日期时间序列化,反序列化。LocalDateTime 默认序列化为数组问题。
·
springboot3.0已经出来几个月了,最低支持jdk17,做个项目练下手
发现LocalDateTime序列化后返回的是一个数组。
就想着配置一个全局的序列化的配置,默认时间和日期的格式
最近本人开源了一个完全免费基于最新技术栈开发的前后端分离的后台管理系统,如果你在搭建基于springboot3.x发现任何问题,可以关注此开源项目作为参考,该踩得坑都踩过了
XHan Admin 官网
前端代码库
前端代码库
springboot 版本 3.x
package com.sunxh.bms.auth.client.web;
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 com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* spring mvc配置类
* sunxh 2023/2/26
*/
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Resource
private MyWebInterceptor myWebInterceptor;
@Resource
private LoggerInterceptor loggerInterceptor;
/**
* 资源跨域设置
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.maxAge(3600);
}
/**
* 请求拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
WebMvcConfigurer.super.addInterceptors(registry);
loggerInterceptor.addInterceptor(registry);
myWebInterceptor.addInterceptor(registry);
}
/**
* 定义全局默认时间序列化
*/
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DATE_FORMAT = "yyyy-MM-dd";
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.indentOutput(true)
.dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
.simpleDateFormat(DATE_TIME_FORMAT)
.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)))
.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)))
.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.modulesToInstall(new ParameterNamesModule());
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)