Spring Boot fastJSON的使用
springBoot,默认使用的json解析框架是Jackson。

虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖就好。
<!-- fastjson依赖库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
版本可根据自己需要查询,网址:http://mvnrepository.com/
Maven依赖完成之后,我们可以通过两种方式配置fastjon
方法一:启动类继承extends WebMvcConfigurerAdapter,且覆盖configureMessageConverters。
方法二:在启动类中,注入Bean:HttpMessageConverters。
代码如下:
package org.lzm.springbootnew;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.ArrayList;
import java.util.List;
@EnableScheduling //定时任务
@SpringBootApplication
public class SpringbootNewApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {SpringApplication.run(SpringbootNewApplication.class, args);}/** // 方法一:extends WebMvcConfigurerAdapter*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {super.configureMessageConverters(converters);//1、先定义一个convert转换消息的对象FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();//2、添加fastjson的配置信息,比如是否要格式化返回的json数据;FastJsonConfig fastJsonConfig=new FastJsonConfig();fastJsonConfig.setSerializerFeatures(//是否需要格式化SerializerFeature.PrettyFormat);//附加:处理中文乱码(后期添加)List<MediaType> mediaTypeList=new ArrayList<MediaType>();mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);fastConverter.setSupportedMediaTypes(mediaTypeList);//3、在convert中添加配置信息fastConverter.setFastJsonConfig(fastJsonConfig);//4、将convert添加到convertersconverters.add(fastConverter);}/** 方法二:在启动类中,注入Bean:HttpMessageConverters*/@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters(){//1、先定义一个convert转换消息的对象FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();//2、添加fastjson的配置信息,比如是否要格式化返回的json数据;FastJsonConfig fastJsonConfig=new FastJsonConfig();fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//附加:处理中文乱码List<MediaType> fastMedisTypes = new ArrayList<MediaType>();fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);fastConverter.setSupportedMediaTypes(fastMedisTypes);//3、在convert中添加配置信息fastConverter.setFastJsonConfig(fastJsonConfig);HttpMessageConverter<?> converter=fastConverter;return new HttpMessageConverters(converter);}
}
其实代码的核心是相同的,这是调取的方式不同而已。两种方式都可以满足我们对于fastjson的依赖使用。
下面使用@JSONField()注解在实体类中进行验证
代码如下。
@JSONField(format = “yyyy-MM-dd”)
private Date birthday;
Controller中代码如下。
@RequestMapping(“/save”)
public Student save(){
Student student=new Student();
student.setName(“婷婷”);
student.setAge(23);
student.setSex(“女”);
student.setBirthday(new Date());
studentService.save(student);
return student;
}
浏览器返回结果:
{
“age”:23,
“birthday”:”2018-07-10”,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}
除此之外,我们还可以通过@JSONField(serialize = false)来决定字段的显示与否。设置如下。
@JSONField(serialize = false)
private Date birthday;
如果这样设置浏览器返回结果如下,birthday将不再显示。
{
“age”:23,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}
Spring Boot fastJSON的使用的更多相关文章
- Spring boot FastJson
介绍:FastJson 是ailibaba 的一款解析Json的开源框架 使用方式1 引入jar 包 <dependency> <groupId>com.alibaba& ...
- 在spring boot环境中使用fastjson + redis的高速缓存技术
因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...
- spring boot 之fastJson的使用(二)
昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...
- spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法
spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...
- spring boot (二):使用fastJson解析json数据
如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...
- spring boot配置使用fastjson
一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- FastJson/spring boot: json输出方法二
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
- FastJson/spring boot: json输出
1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...
随机推荐
- BitMap算法详解
所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来存储数据,因此可以大大节省存储空间. 基本思想: 这此我用一个简单的例子来详细 ...
- 关于MySQL连接抛出Authentication Failed错误分析
[问题描述] 在应用端,偶尔看到有如下报错: Authentication to host 'xxxx' for user 'yyyy' using method 'mysql_native_pass ...
- RabbitMq qos prefetch 消息堵塞问题
mq是实现代码扩展的有利手段,个人喜欢用概念来学习新知识,介绍堵塞问题的之前,先来段概念的学习. ConnectionFactory:创建connection的工厂类 Connection: 简单理解 ...
- keepalived安装配置实战心得(实现高可用保证网络服务不间断)
keepalived安装配置实战心得(实现高可用保证网络服务不间断) 一.准备2台虚拟机 安装的系统是:centos-release-7-1.1503.el7.centos.2.8.x86_6 ...
- (转)mysql原生在线ddl和pt-osc原理解析
原文:http://blog.csdn.net/zengxuewen2045/article/details/52017247 https://github.com/mysql-inception/i ...
- C++实现的字符串模糊匹配
C++基本没有正则表达式功能,当然像Boost里提供了正则.本文来源于博客园园友的一篇文章,请看: C/C++ 字符串模糊匹配 很早之前就看过这篇文章,原作者的需求很明确.代码实现也很好. 之所以又写 ...
- Java之集合(十四)Hashtable
转载请注明源出处:http://www.cnblogs.com/lighten/p/7426522.html 1.前言 HashTable这个类很奇特,其继承了Dictionary这个没有任何具体实现 ...
- kafka 消费者offset记录位置和方式
我们大家都知道,kafka消费者在会保存其消费的进度,也就是offset,存储的位置根据选用的kafka api不同而不同. 首先来说说消费者如果是根据javaapi来消费,也就是[kafka.jav ...
- excel将内容粘贴到筛选后的可见单元格
默认情况下,筛选后excel表格进行复制粘贴,会贴到隐藏的表格. 可以添加两个辅助列来完成操作:1.在筛选前在表格右边添加"辅助1"列,在第二行输入1,按Ctrl+鼠标左键往下拉到 ...
- JAVA使用Gecco爬虫 抓取网页内容(附Demo)
JAVA 爬虫工具有挺多的,但是Gecco是一个挺轻量方便的工具. 先上项目结构图. 这是一个 JAVASE的 MAVEN 项目,要添加包依赖,其他就四个文件.log4j.properties 加上三 ...