spring boot (二):使用fastJson解析json数据
如果我们想在spring boot中使用第三方的json解析框架:
1)我们需要在pom.xml文件中引入第三方包的依赖;
2)实现方法:
方法1 需要在启动类中继承WebMvcConfigurerAdapter 类,并重写该类的configureMessageConverters方法。
方法2. 我们直接使用@Bean注入第三方的 解析框架。
1、引入fastJson的依赖库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
在1.2.10版本后会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConvert,支持4.2以下版本;
一个是FastJsonHttpMessageConvert4,支持4.2以上版本。
2、配置fastJson(支持两种方法)
(1) 方法一:
<1> 启动类继承 WebMvcConfigurerAdapter 类;
<2> 覆盖方法 configureMessageConverters;
① 启动类代码如下:
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter
{ @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
super.configureMessageConverters(converters);
/*
* 1、需要先定义一个 convert 转换消息对象;
* 2、添加 fastJson 的配置信息,比如: 是否要格式化返回的Json数据;
* 3、在 Convert 中添加配置信息;
* 4、将 convert 添加到 converts 中;
*/ //1、需要先定义一个 convert 转换消息对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加 fastJson 的配置信息,比如: 是否要格式化返回的Json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在 Convert 中添加配置信息;
fastConverter.setFastJsonConfig(fastJsonConfig); //4、将 convert 添加到 converts 中;
converters.add(fastConverter); } public static void main(String[] args)
{
/*
* 在main方法中进行启动App类的应用程序
*/
SpringApplication.run(App.class, args);
}
}
② Controller的接口代码如下:
@RestController
public class HelloController
{/**
* 返回的JSON数据
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo()
{
Demo demo = new Demo();
demo.setId(1);
demo.setName("zhangsan");
demo.setCreateTime(new Date());
return demo;
}
}
③ 测试代码:
在Demo实体类中的createTime属性,使用 @JSONField 注解格式化Date类型为"yyyy-MM-dd HH:mm:ss"的格式;如下所示:
public class Demo
{
private int id;
private String name; //注解使用的包: com.alibaba.fastjson.annotation.JSONField
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;//创建时间 //getter 和 setter 方法省略
}
在浏览器中打开 http://localhost:8080/getDemo :
若返回的json数据中 "createTime" 的格式为:yyyy-MM-dd HH:mm:ss,则表示使用的是配置的fastJson处理的数据,否则表示的配置的fastJson不生效。
(2) 方法二:
<1> 在启动类中注入Bean:HttpMessageConverters
启动类的代码如下:
@SpringBootApplication
public class App
{
/**
* 使用 @Bean 注入 FastJsonHttpMessageConverter
* @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); //4、
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
} public static void main(String[] args)
{
/*
* 在main方法中进行启动App类的应用程序
*/
SpringApplication.run(App.class, args);
}
}
3、 fastJson的一些使用方法
(1)格式化日期格式:
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;//创建时间
(2)若不想让接口返回一个属性,则设置 @JSONField 的 serialize 属性为false(不进行序列化);
/*
* 不想返回该属性, serialize:是否进行序列化
*/
@JSONField(serialize = false)
private String remarks;//备注信息
spring boot (二):使用fastJson解析json数据的更多相关文章
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- SpringBoot------使用Fastjson解析Json数据
方法一: 1.在pom.xml文件下添加依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId ...
- 66、fastJson 解析json数据时,如果key值不同怎么处理?
在某些场景,你可能需要定制序列化输出,比如说,希望序列化采用之后采用"ID",而不是"id",你可以使用@JSONField这个Annotation. publ ...
- fastjson生成和解析json数据,序列化和反序列化数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- fastjson生成和解析json数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)
fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...
- Java构造和解析Json数据的两种方法详解二
在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别.下面接着介绍用org.json构造和解析Jso ...
- Java构造和解析Json数据的两种方法详解二——org.json
转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html 在www.json.org上公布了很多JAVA下的jso ...
随机推荐
- Android文档 学习目录
Building Your First App After you've installed the Android SDK, start with this class to learn the b ...
- 吴裕雄 python神经网络 水果图片识别(3)
import osimport kerasimport timeimport numpy as npimport tensorflow as tffrom random import shufflef ...
- 编程四剑客sed-2019.2.20
sed [-Options] [‘Commands’] filename; sed工具默认处理文本,文本内容输出屏幕已经修改,但是文件内容其实没有修改,需要加-i参数即对文件彻底修 ...
- PR(Precision-Recall)曲线和mAP指标
来自: https://www.zhihu.com/question/41540197 https://www.douban.com/note/518998773/ 作者:水哥链接:https://w ...
- 编译模块的Makefile解析
Makefile # if not defined KERNELRELEASE, command is running from command line,need invoke kbuild sys ...
- 1.3.8、CDH 搭建Hadoop在安装之前(端口---Apache Flume和Apache Solr使用的端口)
Apache Flume和Apache Solr使用的端口 Apache Flume用于与Apache Solr通信的端口可能会有所不同,具体取决于您的配置以及是否使用安全性(例如,SSL).使用Fl ...
- ssh 使用 aws
使用 PuTTY 从 Windows 连接到 Linux 实例 启动您的实例之后,您可以连接到该实例,然后像使用您面前的计算机一样来使用它. 注意 启动实例后,需要几分钟准备好实例,以便您能连接到实例 ...
- JMeter学习(三)元件的作用域与执行顺序(转载)
转载自 http://www.cnblogs.com/yangxia-test 1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它 ...
- 九:python 对象类型详解五:元组
一:元组: 1,简单介绍:元组由简单的对象组构成.元组与列表非常类似,只不过元组不能在原处修改(它们)是不可变的,并且通常写成圆括号中的一系列项.虽然元组不支持任何方法调用,但元组具有列表的大多数属性 ...
- centos 6.9 NTP基准时间服务器配置
时间服务器端 yum install ntp -y vim /etc/ntp.conf 增加允许客户端访问 restrict 192.168.0.0 mask 255.255.0.0 nomodify ...