出现$ref的原因及解决方案

出现$ref的原因及解决方案

$ref的产生原因

(1)重复引用:一个集合/对象中的多个元素/属性都引用了同一个对象

(2)循环引用:集合/对象中的多个元素/属性在相互引用导致循环

针对fastjson的处理

fastjson作为一款序列化引擎,不可避免的会遇到循环引用的问题,为了避免StackOverflowError异常,fastjson会对引用进行检测

局部解决法:

JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);

全局解决法:

spring项目:

复制代码

<mvc:annotation-driven>  
        <mvc:message-converters register-defaults="true">  
            <bean  
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <array>  
                        <value>text/html;charset=UTF-8</value>  
                    </array>  
                </property>  
                <property name="features">  
                    <array>  
                        <value>WriteMapNullValue</value>  
                        <value>WriteNullStringAsEmpty</value>  
                        <!-- 全局关闭循环引用检查,最好是不要关闭,不然有可能会StackOverflowException -->
                        <value>DisableCircularReferenceDetect</value>
                    </array>  
                </property>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>  

复制代码

springboot项目:

复制代码

public class FastJsonHttpMessageConverterEx extends FastJsonHttpMessageConverter{
    public FastJsonHttpMessageConverterEx(){
        //在这里配置fastjson特性(全局设置的)
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    //自定义时间格式
        //fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);  //正常转换null值
        //fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);     //关闭循环引用
        this.setFastJsonConfig(fastJsonConfig);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return super.supports(clazz);
    }
}

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    .....
    @Bean
    public FastJsonHttpMessageConverterEx fastJsonHttpMessageConverterEx(){
        return new FastJsonHttpMessageConverterEx();
    }
}

复制代码

注意事项:

FastJson提供了SerializerFeature.DisableCircularReferenceDetect这个序列化选项,用来关闭引用检测。关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致StackOverflowError异常。

针对于mybatis,自身默认开启一级缓存,在session范围内,参数相同的sql查询会查出同一对象,容易引发此问题

创建新的对象,把要有重复需要的对象的属性复制给新对象,新对象再添加到json那里。

作者:涛先森の日常
出处:https://www.cnblogs.com/it-taosir/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!您提到在使用JSON.toJSONString(disassembleActivateIdParam)时报错,错误信息是{"$ref":"$.new_product_nums[0]"}。这个错误通常是由于循环引用导致的。 在Java中,JSON序列化工具通常会遇到循环引用的情况,即对象之间相互引用,导致无限递归序列化。为了解决这个问题,通常需要使用特定的配置或注解来告诉序列化工具如何处理循环引用。 您可以尝试以下几种方法来解决这个问题: 1. 使用Jackson库的@JsonIdentityInfo注解:在您的对象类上使用@JsonIdentityInfo注解,指定一个属性来标识对象的唯一性。例如: ```java @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class YourClass { // ... } ``` 这将告诉Jackson库使用对象的"id"属性来标识对象的唯一性,从而避免循环引用。 2. 使用Jackson库的@JsonManagedReference和@JsonBackReference注解:如果您的对象之间存在一对多或多对一的关系,可以使用@JsonManagedReference和@JsonBackReference注解来标识关系的所有者和反向引用。例如: ```java public class Parent { // ... @JsonManagedReference private List<Child> children; } public class Child { // ... @JsonBackReference private Parent parent; } ``` 这将告诉Jackson库在序列化时只序列化关系的所有者一侧,避免循环引用。 3. 使用Gson库的@Expose注解:如果您使用的是Gson库,可以在对象类上使用@Expose注解,并在序列化时通过excludeFieldsWithoutExposeAnnotation()方法排除未标记的字段。例如: ```java public class YourClass { @Expose private String field1; @Expose private String field2; private String field3; // ... } Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); String json = gson.toJson(disassembleActivateIdParam); ``` 这将告诉Gson库只序列化标记了@Expose注解的字段,避免序列化未标记的字段。 请根据您使用的JSON序列化库和具体情况选择适合的解决方案。希望能帮到您!如有更多问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值