spring boot配置使用fastjson
一、前言
spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的方法。
二、添加依赖
在pom.xml中添加以下依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
三、在启动类(含main函数的那个类)中添加以下代码:
@Bean
public 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);
}
四、测试
假如实体类中有一个Date类型的属性time,则可以使用@JSONField()注解来制定返回的日期格式
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date time;
这样在返回json数据的时候,time就会被解析成“年-月-日 时:分:秒”的形式,还可以通过注解@JSONField(serialize = false)来决定一个字段是否被包含在返回的json中。更多用法可以参考:http://kimmking.github.io/2017/06/06/json-best-practice/
spring boot配置使用fastjson的更多相关文章
- Spring Boot -- 配置切换指南
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- spring boot 配置注入
spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- spring boot配置springMVC拦截器
spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...
- spring boot配置mybatis和事务管理
spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...
- [转] Spring Boot配置多个DataSource
[From] https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...
- Spring boot 配置异步处理执行器
示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Spring boot配置404、500页面
Spring boot 配置404页面很简单,如果你访问的url没有找到就会出现spring boot 提示的页面,很明显Spring boot不用配置就可以显示404页面了. 在template下创 ...
随机推荐
- Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham
Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the ...
- ETL测试
今天让我在休息之余给我的测试朋友介绍一个在我的测试沙龙上的需要和提升技能之一,例如ETL测试(Extract,Transform,and Load,中文名称为数据提取.转换和加载),这篇文章告诉你ET ...
- a标签伪元素选择器
a{ color: black; } /*未访问的链接*/ a:link{ color: red; } /*访问过的链接*/ a:visited{ color: green; } /*鼠标经过时*/ ...
- RabbitMQ问题解决:TCP connection succeeded but Erlang distribution failed
说明 本来是要先把Hystrix 仪表盘更完的,但是出现了Turbine.Dashboard.RabbitMQ整合实现监控. 所以先在学RabbitMq的基本操作,在安装过程中出现了 E:\Rabbi ...
- Elasticsearch之Java实战
资料 http://www.cnblogs.com/kamong/p/6099914.html 搭建Elasticsearch服务器
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- C#反射-Assembly.Load、LoadFrom与LoadFile
反射Demo: public class Person { public int Age; public void SayHello() { Console.WriteLine("Hello ...
- 在mac环境下用QT使用OpenGL,glut,glfw
只需要在新建工程中.pro文件中添加: #opengl glut LIBS+= -framework opengl -framework glut 就可以使用glut了. 继续添加: ##glfw L ...
- win10+anaconda虚拟环境python3.6+cuda9.0+cudnn7+pytorch0.4.1
anaconda下jupyter notebook的打开目录的修改:参考:https://www.cnblogs.com/amberdata/p/7907845.html pytorch官网:http ...
- USB设备被识别流程【转】
转自:http://blog.csdn.net/myarrow/article/details/8286876 USB模块包括usb core,host,hub,device驱动,其中hub会启动一个 ...