1,fastjson的优点:
1.1,fastjson可以只依赖于jdk环境,其他的条件可以不用依赖
1.2, fastjson可以进行序列化和反序列换(所谓的序列化就是将对象转换成对应的json字符串,反序列化就是将json字符串进行转换成对应的对象)
1.3,fastjson进行json转换的的速度远远大于jackjson
实际使用步骤:
一 依赖导入
<!--fastjson依赖添加-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
二 添加配置类
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
* JSON转义字符绑定对象失败解决方案/全局解析器
* 参考 https://blog.youkuaiyun.com/senkoo_lau/article/details/77773093
*/
@Configuration
public class JsonParseConfig extends WebMvcConfigurationSupport {
@Value("${spring.resource.static-locations}")
private String staticLocations;
/**
* 配置fastJson 用于替代jackson
*/
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 1.定义一个convert 转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2 添加fastjson 的配置信息 比如 是否要格式化 返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
//json格式输出
SerializerFeature.PrettyFormat,
// 保留map为空的字段
SerializerFeature.WriteMapNullValue,
// 将String类型的null转成""形式
SerializerFeature.WriteNullStringAsEmpty,
// 将Number类型的null转成0,也可以理解为Integer
SerializerFeature.WriteNullNumberAsZero,
// 将List类型的null转成[],而不是“””
SerializerFeature.WriteNullListAsEmpty,
// Boolean类型的null转成false
SerializerFeature.WriteNullBooleanAsFalse,
// 处理可能循环引用的问题
SerializerFeature.DisableCircularReferenceDetect);
//解决Long转json精度丢失的问题
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
//解决Double科学计数法,可以根据自己的需求,对返回值进行处理
fastJsonConfig.setSerializeFilters(new ValueFilter() {
@Override
public Object process(Object o, String s, Object o1) {
//s:key o1:value
if (o1 instanceof BigDecimal || o1 instanceof Double || o1 instanceof Float) {
BigDecimal bigDecimal = new BigDecimal(o1.toString());
return bigDecimal;
}
if (o1 != null && StringUtils.equles(o1.toString,"[]")) {
return "";
}
return o1;
}
});
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
/**
* 显示Swagger空白页面
* <p>
* 原因分析: 在访问http://localhost:8080/swagger-ui.html 时,这个swagger-ui.html相关的所有前端静态文件都在springfox-swagger-ui-2.6.1.jar里面。
* Spring Boot自动配置本身不会自动把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。我们加上以下映射即可
*
* @param registry
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/**")
.addResourceLocations(staticLocations.split(","));
}
}
然后就可以使用了