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 {

  1. public static void main(String[] args) {
  2. SpringApplication.run(SpringbootNewApplication.class, args);
  3. }
  4. /*
  5. * // 方法一:extends WebMvcConfigurerAdapter
  6. */
  7. @Override
  8. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  9. super.configureMessageConverters(converters);
  10. //1、先定义一个convert转换消息的对象
  11. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
  12. //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
  13. FastJsonConfig fastJsonConfig=new FastJsonConfig();
  14. fastJsonConfig.setSerializerFeatures(
  15. //是否需要格式化
  16. SerializerFeature.PrettyFormat
  17. );
  18. //附加:处理中文乱码(后期添加)
  19. List<MediaType> mediaTypeList=new ArrayList<MediaType>();
  20. mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
  21. fastConverter.setSupportedMediaTypes(mediaTypeList);
  22. //3、在convert中添加配置信息
  23. fastConverter.setFastJsonConfig(fastJsonConfig);
  24. //4、将convert添加到converters
  25. converters.add(fastConverter);
  26. }
  27. /*
  28. * 方法二:在启动类中,注入Bean:HttpMessageConverters
  29. */
  30. @Bean
  31. public HttpMessageConverters fastJsonHttpMessageConverters(){
  32. //1、先定义一个convert转换消息的对象
  33. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
  34. //2、添加fastjson的配置信息,比如是否要格式化返回的json数据;
  35. FastJsonConfig fastJsonConfig=new FastJsonConfig();
  36. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  37. //附加:处理中文乱码
  38. List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
  39. fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
  40. fastConverter.setSupportedMediaTypes(fastMedisTypes);
  41. //3、在convert中添加配置信息
  42. fastConverter.setFastJsonConfig(fastJsonConfig);
  43. HttpMessageConverter<?> converter=fastConverter;
  44. return new HttpMessageConverters(converter);
  45. }

}
其实代码的核心是相同的,这是调取的方式不同而已。两种方式都可以满足我们对于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的使用的更多相关文章

  1. Spring boot FastJson

    介绍:FastJson 是ailibaba 的一款解析Json的开源框架 使用方式1 引入jar 包 <dependency>    <groupId>com.alibaba& ...

  2. 在spring boot环境中使用fastjson + redis的高速缓存技术

    因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...

  3. spring boot 之fastJson的使用(二)

    昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...

  4. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...

  5. spring boot (二):使用fastJson解析json数据

    如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...

  6. spring boot配置使用fastjson

    一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...

  7. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  8. FastJson/spring boot: json输出方法二

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  9. FastJson/spring boot: json输出

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

随机推荐

  1. elasticsearch索引路径规则

    Path to data on disk In prior versions of Elasticsearch, the path.data directory included a folder f ...

  2. python学习笔记03-用户输入

    name=Input(“your name”)  此处接受的数据类型为字符串 整数转字符串  str(123) 整数转字符串  int(“123”)

  3. 【GDKOI2017】 两个胖子萌萌哒 小学奥数题

    题目大意:给你一个$n\times m$的网格,你要在这个网格上画三角形. 三角形的顶点只能在网格的整点上,且至少有一条边平行于$x$或$y$轴,且三角形面积为整数.问你能画多少个不同的三角形. 两个 ...

  4. sql server 2012 数据库日志文件过大,怎么缩小?

    最近发现网站不能访问,原因数据库服务器磁盘剩余空间没了.再细查发现日志文件占用了70%,收缩日志文件失败. 在网上查找原因,是没有备份不能收缩日志文件. 临时解决的方式: 备份事务日志,再收缩日志文件 ...

  5. Linux文件目录管理

    Linux文件目录管理 文件的路径 路径: . 表示当此层目录 .. 表示上一层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的文件夹 ~account 代表accou ...

  6. Redis笔记(三):Redis常用命令

    连接测试 连接本地服务器 语法 $ redis-cli 实例 启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis ...

  7. 自动换行的两种代码(C#)

    最近有个需求,需要将C# winform中的listBox中的内容自动换行, 其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题, 就是textBox中的文字会 ...

  8. Git的gitattributes文件详解

    转自:Git的gitattributes文件详解 Git的gitattributes文件是一个文本文件,文件中的一行定义一个路径的若干个属性. 1. gitattributes文件以行为单位设置一个路 ...

  9. Node.js进程管理之Process模块

    在前面Node.js事件运行机制也有提到,Node.js应用在单个线程运行,但是现在大部分服务器都是多处理器,为了方便使用多个进程,Node.js提供了3个模块.Process模块提供了访问正在运行的 ...

  10. cordova打包APK,SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...

    javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...