github地址:https://github.com/xuzhipeng1028/spring-boot-fastjson
1、引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
2、自定义WebMvcConfigurer
package com.xzp.springboot.fastjson.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* 自定义WebMvcConfigurer
* @author xuzhipeng
* @date 2022/2/18
*/
@Configuration
public class CustomizedWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//删除jackson的消息转换器
converters.removeIf(item -> item instanceof MappingJackson2HttpMessageConverter);
//定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//在converter中添加配置信息
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
//fastjson转换器必须放在StringHttpMessageConverter之后,不然接口返回值如果是字符串,会多加一对双引号
converters.add(fastJsonHttpMessageConverter);
}
}
网友评论