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 ...
随机推荐
- C# 集合转换为DataTable
该类就用了几个类型,如int,int?,string,所以其它类型就先没管. 用到的类: public class tb_Projects { public int ProID { get; set; ...
- C# 反射获取所有视图
原地址:忘了 controller 的 action 加上属性 [System.ComponentModel.Description("菜单列表")] 且 返回值为 Syste ...
- PR(Precision-Recall)曲线和mAP指标
来自: https://www.zhihu.com/question/41540197 https://www.douban.com/note/518998773/ 作者:水哥链接:https://w ...
- NYOJ44-子串和-(dp||思维)
题目描述: 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最大,其中,1<=x<=y<=n. 输入描述: 第一行是一个整 ...
- 用工厂模式和策略模式优化过多的if-else
多个if-else代码: @RunWith(SpringRunner.class) @SpringBootTest public class EducationalBackgroundTest { p ...
- JQuery 基本知识
一.简介 JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, ...
- mysql 模糊搜索
[mysql 模糊搜索] like,%,_,[],[^] 参考:http://www.jb51.net/article/31904.htm
- mysql里max_allowed_packet的作用
MySQL根据配置文件会限制Server接受的数据包大小.有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置: 代码如下: show VAR ...
- 【Django】django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
今天创建APP的时候报这个错误django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you in ...
- hibernate中调用query.list()而出现的黄色警告线
使用hibernate的时候会用到hql语句查询数据库, 那就一定会用到query.list();这个方法, 那就一定会出现一个长长的黄色的警告线, 不管你想尽什么办法, 总是存在, 虽然说这个黄色的 ...