当前位置: 首页 > news >正文

SpringBoot WebMvcConfigurer使用Jackson统一序列化格式化输出

常用json数据格式化有:
1、Long转String防止JS精度丢失
2、时间日期类型的统一格式化

可以使用注解实现json数据格式化:

    /*** 主键 将long使用String序列化*/@JsonSerialize(using = ToStringSerializer.class)@JSONField(serializeUsing = ToStringSerializer.class)private Long id;/*** 时间 日期类格式 注解*/@JSONField(format = "yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private LocalDateTime createTime;

使用注解方式非常繁重,直接在WebMvcConfigurer统一配置实现接口数据响应的格式化输出。

springboot 统一格式化输出简单实现方式;
1、Jackson中ObjectMapper序列化配置

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;/*** 全局 jackson 序列化配置** @author Yang douya* @date 2025/5/29 9:13*/
@Slf4j
@Configuration
public class JacksonConfig {public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";//    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";/*** 创建Jackson对象映射器** @param builder Jackson对象映射器构建器* @return ObjectMapper*/@Primary@Bean("objectMapper")public ObjectMapper getJacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {ObjectMapper objectMapper = builder.createXmlMapper(false).build();//序列换成json时,将所有的long变成string.因为js中得数字类型不能包含所有的java long值,超过16位后会出现精度丢失SimpleModule simpleModule = new SimpleModule();// 字符传转为StringsimpleModule.addSerializer(Long.class, ToStringSerializer.instance);simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);// 时间格式化simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));// 反序列化simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));objectMapper.registerModule(simpleModule);//反序列化的时候如果多了其他属性,不抛出异常objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//日期格式处理objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT));return objectMapper;}
}

2、统一响应格式化输出


/*** 系统拦截器及资源配置** @author helloWorld* <p>* 2018年10月20日*/
@EnableWebMvc
@Configuration
@RequiredArgsConstructor
public class WebMvcConfigurerAdapter implements WebMvcConfigurer {private final ObjectMapper objectMapper;/*** Jackson的全局序列化方式* <p>* long 转为 string* LocalData 格式化** @param converters*/@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();converter.setObjectMapper(objectMapper);converters.add(0, converter);converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));}
}

通过WebMvcConfigurer配置接口响应json数据的统一格式化。

相关文章:

  • 带配额的文件系统 第21次CCF-CSP计算机软件能力认证
  • 数字人革新教育:开启智慧教学新时代
  • Linux系统编程收尾(35)
  • 零硬件成本玩转嵌入式通信!嵌入式仿真实验教学平台解锁STM8S串口黑科技
  • keepalived定制日志bug
  • 轻量级swiper插件推荐
  • 2025陕西省赛补题
  • [python] 最大公约数 和 最小公倍数
  • Dungeon Master(POJ-2251)
  • 现代密码学入门 | 现代密码学核心特点介绍
  • DeepSeek-R1 重磅升级,智能体验再进化!
  • antDesignVue中a-upload上传组件的使用
  • 算法打卡第11天
  • 小工具合集
  • 无人机视角海上漂浮物检测与人员救援检测数据集VOC+YOLO格式2903张6类别
  • 2024 CKA模拟系统制作 | Step-By-Step | 18、题目搭建-备份还原Etcd
  • sward V1.1.4版本发布,支持文档审批及文档导出
  • day40python打卡
  • Linux研学-入门命令
  • 经营分析会,财务该怎么做?
  • 做cpa能用什么网站/yandex引擎搜索入口
  • 常熟港口建设费申报网站/怎么做好seo推广
  • 建设投资基金管理有限公司网站/河北搜索引擎优化
  • 姓氏头像在线制作免费生成图片/百度seo优化及推广
  • 西安品牌网站建设服务商/近期时事新闻
  • 微推客/seo技术外包