• fastjson解析出现引用问题


    1.问题描述

     后端返回前端接口数据包含引用数据,如下图所示

     2.原因

    转json时使用这种方式,fastjson自动使用循环引用:

    String content = JSONObject.toJSONString(object);
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    3.fastjson的循环引用

    当一个对象包含另一个对象时,fastjson就会把该对象解析成引用。引用是通过$ref标示的,下面介绍一些引用的描述
    "$ref":".." 上一级
    "$ref":"@" 当前对象,也就是自引用
    "$ref":"$" 根对象
    "$ref":"$.children.0" 基于路径的引用,相当于 root.getChildren().get(0)

    4.解决方案

    对象转为json

    String content = JSONObject.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    json解析为对象,两个方式

    1.全局配置

    1. package com.flowpp.citylights.detection.config;
    2. import com.alibaba.fastjson.serializer.SerializerFeature;
    3. import com.alibaba.fastjson.support.config.FastJsonConfig;
    4. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.web.servlet.FilterRegistrationBean;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.http.MediaType;
    9. import org.springframework.http.converter.HttpMessageConverter;
    10. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
    11. import org.springframework.stereotype.Component;
    12. import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    13. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    14. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    15. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    16. import java.nio.charset.StandardCharsets;
    17. import java.util.ArrayList;
    18. import java.util.Iterator;
    19. import java.util.List;
    20. /**
    21. * ps: 不能同时存在WebMvcConfigurationSupport和WebMvcConfigurer
    22. * 否则会导致继承WebMvcConfigurationSupport的配置类失效
    23. *
    24. * @description mvc配置
    25. **/
    26. @Component
    27. public class WebMvcConfig extends WebMvcConfigurationSupport {
    28. @Override
    29. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    30. configurer.enable();
    31. }
    32. @Override
    33. public void configureMessageConverters(List> converters) {
    34. //先干掉jackson
    35. Iterator> iterator = converters.iterator();
    36. while (iterator.hasNext()) {
    37. HttpMessageConverter converter = iterator.next();
    38. if (converter instanceof MappingJackson2HttpMessageConverter) {
    39. iterator.remove();
    40. }
    41. }
    42. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    43. //自定义配置...
    44. FastJsonConfig config = new FastJsonConfig();
    45. config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    46. config.setCharset(StandardCharsets.UTF_8);
    47. config.setSerializerFeatures(
    48. //禁止循环引用和重复引用
    49. SerializerFeature.DisableCircularReferenceDetect,
    50. SerializerFeature.WriteMapNullValue,
    51. SerializerFeature.PrettyFormat,
    52. SerializerFeature.WriteNullListAsEmpty,
    53. SerializerFeature.WriteNullStringAsEmpty
    54. );
    55. converter.setFastJsonConfig(config);
    56. List fastMediaType = new ArrayList<>();
    57. MediaType mediaType = MediaType.parseMediaType("text/html;charset=UTF-8");
    58. fastMediaType.add(mediaType);
    59. fastMediaType.add(MediaType.APPLICATION_JSON);
    60. converter.setSupportedMediaTypes(fastMediaType);
    61. converters.add(0, converter);
    62. }
    63. }

    2.局部配置

    在属性上添加注解

    1. /**
    2. * 控制范围
    3. */
    4. @JSONField(serialzeFeatures = SerializerFeature.DisableCircularReferenceDetect)
    5. private List controlRange;

     以上都配置完成了之后,我们项目仍然不生效,这是最让人纠结的事,为此郁闷了一天。翻看了网上所有资料,这种配置都没有问题,因此查看了fastjson的最新版本和项目中版本对比。一般会选择使用者较多且较新的版本。我们项目使用的是2.0.6版本,这个是dubbo-spring-boot-starter的2.7.8版本自动引用的,挺坑人的。换个fastjson的2.0.7版本就好使了。又是开心的一天~

    1. com.alibaba
    2. fastjson
    3. 2.0.7
  • 相关阅读:
    消息推送平台有没有保证数据不丢?
    手机越用越慢?试试这4个秘籍,让手机流畅如新
    Linux帧缓冲注册OLED驱动
    ML学习笔记--Word Embedding
    垃圾收集器
    Linux学习笔记——Shell和Bash
    金九银十招聘季, APP测试面试题助你拿高薪Offer
    leetcode134.加油站 贪心法求解 (c++版本)
    vue学习(基础1)
    git fatal: detected dubious ownership in repository 解决方法
  • 原文地址:https://blog.csdn.net/zhangxl123liang/article/details/126279493