78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
2017-02-13 17:09
886 查看
【原创文章,转载请注明出处】
个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析呢?
引入fastjson依赖库:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。
配置fastjon
支持两种方法:
第一种方法就是:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters
具体代码如下:
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = newFastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
第二种方法:
(1)在App.java启动类中,注入Bean : HttpMessageConverters
具体代码如下:
package com.kfit;
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.converter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args)
{
SpringApplication.run(ApiCoreApp.class, args);
}
}
那么这时候在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的话,那么恭喜你配置成功了,其中JSONField的包路径是:com.alibaba.fastjson.annotation.JSONField。
个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析呢?
引入fastjson依赖库:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。
配置fastjon
支持两种方法:
第一种方法就是:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters
具体代码如下:
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = newFastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
第二种方法:
(1)在App.java启动类中,注入Bean : HttpMessageConverters
具体代码如下:
package com.kfit;
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.converter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args)
{
SpringApplication.run(ApiCoreApp.class, args);
}
}
那么这时候在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的话,那么恭喜你配置成功了,其中JSONField的包路径是:com.alibaba.fastjson.annotation.JSONField。
相关文章推荐
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Spring Boot 学习4--完美使用FastJson解析Json数据
- Spring Boot 完美使用FastJson解析Json数据
- Spring Boot完美使用FastJson解析JSON数据(十三)
- 【Spring Boot】完美使用FastJson解析JSON数据
- 【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
- (二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- Spring Boot使用FastJson解析JSON数据的方法
- Spring Boot使用FastJson解析JSON数据
- spring boot使用fastJson解析数据
- SpringBoot 之 第三方Json解析框架FastJson的使用
- 4. 使用别的json解析框架【从零开始学Spring Boot】
- Spring Boot与Mybatis 借助Fastjson快速完成数据解析入库
- Spring Boot 使用第三方json解析 : fastjson
- 4. 使用别的json解析框架【从零开始学Spring Boot】
- (4)Spring Boot使用别的json解析框架【从零开始学Spring Boot】