springboot使用fastJson作为json解析框架

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" }

原文地址:https://www.cnblogs.com/xujie09/p/8461483.html