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 ...
随机推荐
- 利用lipo编译合并iPhone模拟器和真机通用的静态类
利用lipo编译合并iPhone模拟器和真机通用的静态类 如何编译静态类库,而且现在网上也有很多的教程,现在问题时我们编译好了的静态类库会时两个版本的.a文件,分别用于模拟器和iPhone真迹,因此M ...
- 吴裕雄 python深度学习与实践(4)
import numpy,math def softmax(inMatrix): m,n = numpy.shape(inMatrix) outMatrix = numpy.mat(numpy.zer ...
- vue -- 九宫格抽奖
html: <div class="line_item" :class="index == 1 ? 'active' : 'white_item'"> ...
- js异步导致的错误
没想到jquery的$.each也是异步,本身是循环验证数据,然后再提交数据,但是发现验证和提交一起发生了. 技术还不到位,所以 在定义了一个变量,var step = 0; 每循环一次step自增, ...
- 原生js实现一个简单的轮播图
想锻炼一下自己的原生js能力可以从写一个轮播图开始,轮播图的运用想必大家都知道吧,好了废话不多说,开始记笔记了,一些需要注意的点,我都在代码中标注了 首先是构造html: <div id=&qu ...
- eCharts 折线图,动态绑定数据不更新图表的问题,
官方文档 : http://echarts.baidu.com/tutorial.html npm install echarts --save let lineChart = echarts.ini ...
- Docker 网络不通的解决方法
表现是: docker主机内部网络正常,与其它主机的连接失效,其它主机不能连接docker主机上映射的端口,docker内部也无法连接外部主机. 执行docker info,可以看到一些警告. 可在不 ...
- JAVA jar 参数
-client to select the "client" VM -server to select the "server" ...
- WAS 手动删除server
有时候WPS Server上的应用会一直start不起来,尝试卸载也会失败.在这种情况下,我们可以手动删除这个文件. 步骤如下: 1. Stop server 2. 进入$Profile_instal ...
- CentOS 下搭建Hudson
1.下载Hudson安装包 wget http://ftp.jaist.ac.jp/pub/eclipse/hudson/war/hudson-3.3.3.war 2.执行 java -jar hud ...