SpringBoot整合FastJson过程解析
2019-12-06 12:10
921 查看
这篇文章主要介绍了SpringBoot整合FastJson过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一、Maven依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.33</version> </dependency>
二、配置类
@Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 使用fastjson代替jackson * * @param */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { /* 先把JackSon的消息转换器删除. 备注: (1)源码分析可知,返回json的过程为: Controller调用结束后返回一个数据对象,for循环遍历conventers,找到支持application/json的HttpMessageConverter,然后将返回的数据序列化成json。 具体参考org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor的writeWithMessageConverters方法 (2)由于是list结构,我们添加的fastjson在最后。因此必须要将jackson的转换器删除,不然会先匹配上jackson,导致没使用fastjson */ for (int i = converters.size() - 1; i >= 0; i--) { if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) { converters.remove(i); } } FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //自定义fastjson配置 FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures( SerializerFeature.WriteMapNullValue, // 是否输出值为null的字段,默认为false,我们将它打开 SerializerFeature.WriteNullListAsEmpty, // 将Collection类型字段的字段空值输出为[] SerializerFeature.WriteNullStringAsEmpty, // 将字符串类型字段的空值输出为空字符串 SerializerFeature.WriteNullNumberAsZero, // 将数值类型字段的空值输出为0 SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect // 禁用循环引用 ); fastJsonHttpMessageConverter.setFastJsonConfig(config); // 添加支持的MediaTypes;不添加时默认为*/*,也就是默认支持全部 // 但是MappingJackson2HttpMessageConverter里面支持的MediaTypes为application/json List<MediaType> fastMediaTypes = new ArrayList<>(); MediaType mediaType = MediaType.parseMediaType("text/html;charset=UTF-8"); fastMediaTypes.add(mediaType); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); // fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8")); converters.add(fastJsonHttpMessageConverter); } }
三、使用
@JSONField(serialize=false) private String delFlag; // 数据删除标记, 0-已删除 1-有效
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Spring Boot与Mybatis 借助Fastjson快速完成数据解析入库
- Spring Boot 完美使用FastJson解析Json数据
- springboot添加fastjson第三方json解析工具方法(默认jackjson)
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- spring-boot实现FastJson解析json数据
- Spring Boot 整合 Shiro+Thymeleaf过程解析
- Spring Boot使用FastJson解析JSON数据的方法
- spring-boot实现FastJson解析json数据
- spring boot 配置fastjson解析json
- spring boot 整合 resteasy+fastjson
- 【Spring-boot 】FastJson对json数据进行解析(类型转换)
- Spring Boot使用FastJson解析JSON数据
- springboot整合redis集群过程解析
- spring boot使用fastJson解析数据
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Springboot整合MybatisPlus的实现过程解析
- Spring Boot 使用第三方json解析 : fastjson
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】