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. Sublime Text shift+ctrl妙用(转载)

    1 :按住shift+ctrl然后按←或→可快速选中一行中的某一部分,相当于双击鼠标选中. 当你想在代码末尾加注释的话,这个方法很好用 输入文字->光标移到文字末尾->按住shift+ct ...

  2. js之作用域

    1.什么是作用域 作用域是用于收集存储维护变量,以及当前执行代码声明的变量所拥有的权限, 例如 : function foo(a){ console.log(a); --------    1    ...

  3. 下载apache-tomcat-9.0.17-windows-x64及安装以及用途

    首先我们先去这个网站下载http://www.apache.org/,进入Tomcat,点击Tomcat9 下载64-bit Windows_zip 当我们下载好了之后解压,把多余的文件删除掉,也可以 ...

  4. CSS中的BFC详解

    引言: 这篇文章是我对BFC的理解及总结,带你揭开BFC的面纱.你将会知道BFC是什么,形成BFC的条件,BFC的相关特性,以及他的实际应用. 一.何为BFC BFC(Block Formatting ...

  5. 【NOIP2017】逛公园 最短路+DP

    诶,去年场上不会处理$0$的环,只拿了$60$有点可惜. 我们先不管边边权为$0$的边. 我们先跑一次最短路,令$dis[u]$表示从$1$至$u$的最短路的长度. 那么根据题目的要求,从起点走到$u ...

  6. HashMap、HashSet、LinkedHashSet、TreeSet的关系

    类图及说明如下:

  7. python笔记06-----常用模块(time,os,sys,random)

    模块 1. 模块的定义和导入 定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py对应的模块名:test) ...

  8. python笔记05-----函数

    函数 编程序语言中函数定义:函数是逻辑结构化和过程化的一种编程方法 def func(i): # def :定义函数的关键字:func:函数名:()内可以定义形参 i += 1 # 代码块或程序处理逻 ...

  9. springboot-6-整合jdbc

    如果有整合jpa了, 那么在dao中直接, 不需要引入依赖 @Resource private JdbcTempalte jdbcTempalte; 如果没有的话, 就先在pom.xml中加入依赖 & ...

  10. Maven 设置阿里巴巴发布版本仓库

    打开 pom.xml 在里面添加以下代码就能顺利在阿里上面下载所需要的 jar 包文件: <!--设置 Maven 组件仓库 --> <repositories> <!- ...