SpringBoot消息转换器扩展fastjson

在pom.xml中引入fastjson

<dependency >

<groupId >com.alibaba</groupId >

<artifactId >fastjson</artifactId >

<version >1.2.47</version >
</dependency >

配置消息转换器,添加fastjson

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fc = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat );
fc.setFastJsonConfig(fastJsonConfig);
converters.add(fc);
}

创建User实体类

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class User { private String username ; private String password ; private int age ; private int score ; private int gender ; @JSONField(format = "yyyy-MM-dd" ) private Date date ; 创建jsoncontroller @Controller public class JsonController { @RequestMapping("/json") @ResponseBody public User json() { User user = new User(); user.setUsername("zhangsan"); user.setPassword("123456"); user.setAge(20); user.setScore(90); user.setGender(1); user.setDate(new Date()); return user; } } 启动项目访问http://localhost:端口号/json |

相关推荐
huangdong_3 小时前
电商平台图片URL原图转换技术深度解析:从缩略图到高清原图的完整方案
java·后端·spring
記億揺晃着的那天3 小时前
Java 调用外部 Go 程序的实践:ProcessBuilder 在生产环境中的应用
java·golang·processbuilder
JAVA面经实录9173 小时前
Java 数据结构与算法 (终极完整学习文档)
java·数据结构·算法
llz_1123 小时前
web-第四次课后作业
前端·spring boot·web
JAVA面经实录9174 小时前
操作系统面试题
java·服务器·数据库·计算机网络·面试
一杯奶茶¥4 小时前
基于springboot的失物招领管理系统带万字文档 校园失物招领管理系统 失物认领管理系统java springboot vue
java·vue.js·spring boot·java项目
不能只会打代码4 小时前
边缘视频分析平台的架构设计与性能优化——从750ms到190ms的调优之路
java·spring boot·redis·性能优化·边缘计算·物联网竞赛
小刘|4 小时前
Spring AI Alibaba 集成和风天气 API 实战
java·服务器·前端
KANGBboy4 小时前
java知识五(继承)
java·开发语言
AI人工智能+电脑小能手5 小时前
【大白话说Java面试题 第117题】【并发篇】第17题:线程有几种状态,之间如何转换?
java·开发语言·面试