springboot使用fastJson作为json解析框架

springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用

〇、搭建springbboot基础环境

一、添加依赖

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>

二、在app.class(启动类)中的配置

配置方式一(通过继承的方式)

1、启动类继承WebMvcConfigurerAdapter

2、重写configureMessageConverters方法

	@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/*
* 1、需要先定义一个convert转换消息的对象
* 2、添加fastJson的配置信息,比如:是否要格式化返回json数据
* 3、在convert中添加配置信息
* 4、将convert添加到converters当中
*
*/
//1、需要先定义一个·convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息,比如 是否要格式化返回json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters当中.
converters.add(fastConverter);
}

配置方式二(通过@Bean注入的方式)

@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);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}

二、以第一种配置方式,测试验证一下:

准备一个Controller

import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xujie.pojo.Demo; @RestController // 包括了@ResponseBody和Controller
public class UserController { @GetMapping("/test")
public Demo test() {
Demo demo = new Demo();
demo.setId(1);
demo.setCreateTime(new Date());
demo.setName("demo22");
demo.setRemarks("这里是备注信息,不应该返回");
return demo;
}
}

启动类写法

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication
public class UserApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
} @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//1、需要先定义一个·convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息,比如 是否要格式化返回json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、将convert添加到converters当中.
converters.add(fastConverter);
}
}

pojo类

import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField; public class Demo {
private int id;
private String name; //com.alibaba.fastjson.annotation.JSONField
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;//创建时间. /*
* serialize:是否需要序列化属性.
*/
@JSONField(serialize=false)
private String remarks;//备注信息. public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

浏览器测试

输入地址:http://localhost:8080/test

返回:

{ "createTime":"2018-02-23 12:06", "id":1, "name":"demo22" }

springboot使用fastJson作为json解析框架的更多相关文章

  1. 十七、springboot配置FastJson为Spring Boot默认JSON解析框架

    前提 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 1.引入fastjson依赖库: maven: <dependen ...

  2. Spring Boot默认的JSON解析框架设置

    方案一:启动类继承WebMvcConfigurerAdapter,覆盖方法configureMessageConverters ... @SpringBootApplication public cl ...

  3. 移动架构-json解析框架

    JSON在现在数据传输中占据着重要地位,相比于xml,其解析和构成都要简单很多,第三方的解析框架也不胜枚举,这里之所以要自定义一个json解析框架,一方面是更好的了解json解析过程,另一方面是有时候 ...

  4. 将SpringBoot默认Json解析框架jackson替换成fastjson

    步骤一:引入依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...

  5. (4)Spring Boot使用别的json解析框架【从零开始学Spring Boot】

    此文章已经废弃,请看新版的博客的完美解决方案: 78. Spring Boot完美使用FastJson解析JSON数据[从零开始学Spring Boot] http://412887952-qq-co ...

  6. FastJson将json解析成含有泛型对象,内部泛型对象再次解析出错的解决办法(Android)

    折腾小半天的问题,这里先感谢一下深圳的小伙子,远程帮我搞,虽然也没有搞出来==========FUCK 声明:Android开发下发生此异常,Java开发下并不会有这个问题 异常重现 简单说一下抛出异 ...

  7. 4. 使用别的json解析框架【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51585921 此文章已经废弃,请看新版的博客的完美解决方案: 78. Spring Boo ...

  8. Android 利用fastjson进行json解析

    package com.example.FastJson.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.Typ ...

  9. Android总结之json解析(FastJson Gson 对比)

    前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...

随机推荐

  1. Git Gerrit Repo User Manual

                      Git Repo Gerrit User Manual Revision History   Revision # Description Date Author ...

  2. Sqoop数据迁移工具

    一.概述 sqoop 是 apache 旗下一款“ Hadoop 和关系数据库服务器之间传送数据”的工具. 导入数据: MySQL, Oracle 导入数据到 Hadoop 的 HDFS. HIVE. ...

  3. 最近遇到的DISCUZ一些问题解决方法

    “抱歉,您的请求来路不正确或表单验证串不符,无法提交” 打开“source\class\helper\helper_form.php”, 然后把“$_GET[‘formhash’] == formha ...

  4. 学习 opencv---(11)OpenC 边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器

    本篇文章中,我们将一起学习OpenCV中边缘检测的各种算子和滤波器——Canny算子,Sobel算子,Laplace算子以及Scharr滤波器.文章中包含了五个浅墨为大家准备的详细注释的博文配套源代码 ...

  5. (转)ARC指南 - strong、weak指针

    一.简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内存管理,因为编 ...

  6. python学习(九) 网络编程学习--简易网站服务器

    python `网络编程`和其他语言都是一样的,服务器这块步骤为:`1. 创建套接字``2. 绑定地址``3. 监听该描述符的所有请求``4. 有新的请求到了调用accept处理请求` Python ...

  7. hihocoder 1580 dp最大子矩阵和

    题意: 给出n*m的矩阵求最大子矩阵和,要求必须把矩阵中的某一个元素替换成p 代码: //求最大子矩阵和,容易想到压缩之后dp但是这道题要求必须替换一次p,必然优先替换最小的. //这样如果求得的结果 ...

  8. WIFI Direct(Wi-Fi P2P)

    Wi-Fi Direct技术是Wi-Fi产业链向蓝牙技术发起的挑战,它试图完全取代蓝牙 Wi-Fi Direct是一种点对点连接技术,它可以在两台station之间直接建立tcp/ip链接,并不需要A ...

  9. python---Scrapy模块的使用(一)

    Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中. Scrapy 使用了 Twisted异步网络库来处理网络通讯.整体 ...

  10. java项目转换依赖等问题

    最近接手了一个原始的java项目,其实很久没有做了,自从两年前用maven,建立web项目,java project基本上就没有弄个,突然的接手,发现自己好多不足,可能对于一开始就做这样的容易,但是对 ...