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 |

相关推荐
Fuyo_111910 分钟前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
Sarvartha28 分钟前
Date、LocalDate、LocalTime、LocalDateTime、DateTimeFormatter类
java
阿俊-全栈开发1 小时前
LikeShop 多商户 Java 版商城微服务模块拆分与部署架构演进实践
java·微服务·架构·likeshop开源商城·多商户源码
Wang's Blog1 小时前
Java 项目实战:交友项目 - 联系人列表
java·开发语言·交友
我命由我123451 小时前
访客系统形式(有用户概念、无用户概念)
java·开发语言·前端·javascript·python·产品运营·产品经理
香蕉大香蕉(06敬畏市场)2 小时前
MySQL 单点详解(01)
java·数据库·mysql·面试·博客·实习
LaughingZhu2 小时前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion
niucloud-admin2 小时前
NIUCLOUD PHP SAAS上门家政使用文档:服务保障
java·开发语言·php
IT枫斗者枫哥3 小时前
CompletableFuture失败后返回空列表:异常没了,业务含义也变了
java·后端
知识搬运临时工3 小时前
一个模板把 AEM 拖进 Full GC 死亡螺旋:300% CPU、7 GB 堆、310 万行堆栈
java·aws