一. SpringBoot自带JSON解析工具
通过观察SpringBoot自动引入的jar包可以发现, SpringBoot其实自身已经携带JSON解析工具:Jackson. 如果不想使用Jackson而使用FastJson的话, 需要在SpringBoot中配置FastJson.
二. 步骤
1. 在pom.xml中引入FastJson依赖库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
2. 配置FastJson(两种方式)
方式一:
① SpringBoot启动类继承WebMvcConfigurerAdapter
② 重写configureMessageConverters()方法
package online.bendou.springboot.fastjsondemo;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
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;
/**
* SpringBoot的启动类, 使用@SpringBootApplication修饰
* @author Lx
*
* 继承WebMvcConfigurerAdapter, 重写
*
*/
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
/*
* 1. 需要先定义一个convert转换器对象
* 2. 配置添加fastjson的配置信息, 比如: 是否要格式化返回的json数据;
* 3. 把配置信息添加到convert转换器对象中;
* 4. 解决中文乱码
* 5. 将convert添加到转换器对象当中;
*/
// 1.
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.
fastConverter.setFastJsonConfig(fastJsonConfig);
//4. 解决中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
// 5.
converters.add(fastConverter);
};
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
③ 在实体类中添加FastJson注解校验配置的FastJson是否生效
package online.bendou.springboot.fastjsondemo.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.annotation.JSONField;
@Component
@Entity(name="user_info")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
// 出生日期
@JSONField(format="yyyy-MM-dd")
private Date birthday;
get/set...
}
④ 编写Contoller
package online.bendou.springboot.fastjsondemo.controller;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import online.bendou.springboot.fastjsondemo.entity.User;
@RestController
public class FastJsonController {
@GetMapping("/user")
public Object findAll(){
User user = new User();
user.setId(1);
user.setName("张三");
user.setBirthday(new Date());
return user;
}
}
⑤ 运行启动类, 浏览器访问测试
方式二:
在Application.java启动类中注入Bean: HttpMessageConverters
package online.bendou.springboot.fastjsondemo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
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.web.bind.annotation.RestController;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* 配置FastJson的方式二:
* 在启动类中注入HttpMessageConverters
* @author Lx
*
*/
@RestController
public class Application {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
// 1.创建一个converter对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.创建配置对象
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 3.添加配置
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 4.将配置添加到转换器对象中
fastConverter.setFastJsonConfig(fastJsonConfig);
// 5. 解决中文乱码问题
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(mediaTypes);
// 6.将转换器对象转化为HttpMessageConverter对象
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}