SpringBoot使用FastJson解析JSON数据

1:引入fastjson依赖库

SpringBoot使用FastJson解析JSON数据

2:配置fastjson(支持两种方法)

第一种方法

(1):启动类继承extends WebMvcConfigurerAdapter

(1):覆盖方法configureMessageConverters


SpringBoot使用FastJson解析JSON数据


SpringBoot使用FastJson解析JSON数据


SpringBoot使用FastJson解析JSON数据


SpringBoot使用FastJson解析JSON数据


这时候我们就可以看到SpringBoot已经用我们自己定义的底层fastjson进行数据的解析


第二种方法

1:在SpringBootHelloApplication启动类中,注入bean:HttpMessageConverters

SpringBoot使用FastJson解析JSON数据


总结:我们要使用第三方json解析框架的话:

1.我们需要在pom.xml中引入相对应的依赖;

2.需要在SpringBootHelloApplication.java中继承WebMvcConfigurerAdapter 重写方法configureMessageConverters 添加我们自己定义的json解析框架;

2.1. @Bean注入第三方的json解析框架:

@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);

}