博客
SpringBoot json 解析
2020-07-01 11247 0
简介 Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回 Sprigboot 默认使用的json解析技术框架是jackson 直接返回对象 @RequestMapping("/user") Cat getUser...
Spring Boot为我们良好的提供了我们需要的数据,将数据转化为json格式,然后返回
Sprigboot 默认使用的json解析技术框架是jackson
直接返回对象
@RequestMapping("/user")
Cat getUser() {
Cat cat = new User();
cat.setName("dafeiyu");
cat.setCreateDate(new Date());
return cat;
}
启动访问结果

Spring Boot使用FastJson解析JSON数据
我们要使用第三方的json解析框架的话: 我们需要在pom.xml中引入相应的依赖;
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架;
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/*
* 1、需要先定义一个 convert 转换消息的对象;
* 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
* 3、在convert中添加配置信息.
* 4、将convert添加到converters当中.
*
*/
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters当中.
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
或者 @Bean注入第三方的json解析框架:
@SpringBootApplication
public class Application{
/**
* 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动 访问

标签云
- 分享(4)影视(4)测试(1)云标签(0)markdown(0)SpringBoot(21)SpringBoot启动失败(1)springboot解析json(1)orm(1)java(4)linux(8)防火墙(1)linux防火墙(1)iptables(1)firewall(1)docker(8)创建docker镜像(1)docker安装redis(1)nginx(5)linux命令(1)源码解读(2)git(1)记录(3)酷玩(9)测试公众号(1)小工具(7)酷玩软件(5)ColorPix(1)size(1)身份证生成器(1)源码(2)抖音H5源码(1)仿抖音H5(1)酷玩代码(8)多线程(2)流媒体服务器(1)直播(2)nacos(3)微服务(5)数据库(3)面试(3)zookeeper(1)公众号爬虫(50)爬虫(1)三十而已(1)每日一冷(26)冷兔(12)JDK8新特性(1)lambda详细解析(1)九一八79周年纪念(1)fc(1)jsnes(1)在线玩fc游戏(1)没有比大人更大的人(1)感动(1)生活不易(1)GBA模拟器(1)标签云(1)源码分享(3)动态头像(1)固定url动态图片(1)docker安装mysql(2)navicat链接1251(1)Minio(1)docker安装Minio(1)扫码发短信(1)发短信html(1)网站建设(1)微信刷步(1)wordpress(1)docker安装wordpress(1)酷玩工具(2)Honeyview(1)蜂蜜浏览器(1)SumatraPDF(1)装机工具(1)学习资料(1)小游戏(1)mysql(4)linux安装mysql(1)前端模板(1)dock(1)PowerDesigner(1)破解汉化(1)SpringBoot启动流程(1)SpringBoot自动装载(1)sitemap(1)seo(1)趣味代码(1)fc游戏机(1)小霸王游戏机(1)朋友圈的空调(1)nginx配置黑名单(1)博客嵌入B站视频(1)jQueryTOC(1)Redis面试题(0)Redis(0)吊打面试官(1)mysql8忽略大小写(1)在线玩魔方(1)代码分享(1)Springboot部署windows(1)端口占用(1)QQxml卡片消息发送教程(1)QNotified(1)太极(1)QQ卡片代码(1)九一八(1)远程打卡(1)虚拟定位(1)日记(1)rocketmq(1)消息队列(1)fastdfs(1)office(1)小技巧(1)音乐(1)抖音视频解析(1)无水印下载抖音视频(1)轻量网盘(1)临时邮箱(1)验证码代收(1)Spacedesk(1)Typora(1)视频下载(2)灌篮高手(1)视频流下载(2)散列值(1)数字签名(1)HASH(1)迪迦奥特曼(1)zhihu(2)小说(1)代码优化(1)刷机(1)知乎(1)纪实(1)openssl(1)工作笔记(1)小巷人家(1)视频(1)社保(1)时政(1)AI(1)回表(1)索引覆盖(1)八股文(1)SpringCloud(1)B站(0)AIMV(0)B站(0)AIMV(0)B站(0)AIMV(0)B站(0)AIMV(0)B站(1)AIMV(1)Redis面试题(0)Redis(0)吊打面试官(0)面试(0)Redis面试题(1)Redis(1)吊打面试官(1)面试(1)
文章评论
最近更新
- 11-22 赵本山《Bad Guy》让铁岭再次伟大
- 11-18 16G+512G“公认很香”,目前3款“值得闭眼入”的手机,双11重点考虑
- 11-17 Springboot 属性配置和优先级
- 08-26 微服务相关
- 07-08 在Mysql中,什么是回表,什么是覆盖索引,索引下推
- 05-05 Claude Code 命令大全
- 04-22 从零开始看懂 AI
- 08-07 不是你需要社保,而是社保需要你了
- 06-02 Controller层代码瘦身70%!5招打通任督二脉,效率飙升
- 11-09 《小巷人家》全集视频下载代码
标签云
- 分享(4)影视(4)测试(1)云标签(0)markdown(0)SpringBoot(21)SpringBoot启动失败(1)springboot解析json(1)orm(1)java(4)linux(8)防火墙(1)linux防火墙(1)iptables(1)firewall(1)docker(8)创建docker镜像(1)docker安装redis(1)nginx(5)linux命令(1)源码解读(2)git(1)记录(3)酷玩(9)测试公众号(1)小工具(7)酷玩软件(5)ColorPix(1)size(1)身份证生成器(1)源码(2)抖音H5源码(1)仿抖音H5(1)酷玩代码(8)多线程(2)流媒体服务器(1)直播(2)nacos(3)微服务(5)数据库(3)面试(3)zookeeper(1)公众号爬虫(50)爬虫(1)三十而已(1)每日一冷(26)冷兔(12)JDK8新特性(1)lambda详细解析(1)九一八79周年纪念(1)fc(1)jsnes(1)在线玩fc游戏(1)没有比大人更大的人(1)感动(1)生活不易(1)GBA模拟器(1)标签云(1)源码分享(3)动态头像(1)固定url动态图片(1)docker安装mysql(2)navicat链接1251(1)Minio(1)docker安装Minio(1)扫码发短信(1)发短信html(1)网站建设(1)微信刷步(1)wordpress(1)docker安装wordpress(1)酷玩工具(2)Honeyview(1)蜂蜜浏览器(1)SumatraPDF(1)装机工具(1)学习资料(1)小游戏(1)mysql(4)linux安装mysql(1)前端模板(1)dock(1)PowerDesigner(1)破解汉化(1)SpringBoot启动流程(1)SpringBoot自动装载(1)sitemap(1)seo(1)趣味代码(1)fc游戏机(1)小霸王游戏机(1)朋友圈的空调(1)nginx配置黑名单(1)博客嵌入B站视频(1)jQueryTOC(1)Redis面试题(0)Redis(0)吊打面试官(1)mysql8忽略大小写(1)在线玩魔方(1)代码分享(1)Springboot部署windows(1)端口占用(1)QQxml卡片消息发送教程(1)QNotified(1)太极(1)QQ卡片代码(1)九一八(1)远程打卡(1)虚拟定位(1)日记(1)rocketmq(1)消息队列(1)fastdfs(1)office(1)小技巧(1)音乐(1)抖音视频解析(1)无水印下载抖音视频(1)轻量网盘(1)临时邮箱(1)验证码代收(1)Spacedesk(1)Typora(1)视频下载(2)灌篮高手(1)视频流下载(2)散列值(1)数字签名(1)HASH(1)迪迦奥特曼(1)zhihu(2)小说(1)代码优化(1)刷机(1)知乎(1)纪实(1)openssl(1)工作笔记(1)小巷人家(1)视频(1)社保(1)时政(1)AI(1)回表(1)索引覆盖(1)八股文(1)SpringCloud(1)B站(0)AIMV(0)B站(0)AIMV(0)B站(0)AIMV(0)B站(0)AIMV(0)B站(1)AIMV(1)Redis面试题(0)Redis(0)吊打面试官(0)面试(0)Redis面试题(1)Redis(1)吊打面试官(1)面试(1)


