xieegai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

原因:当开启FastJson的“循环引用检测”特性时,如果生成的多个JSON对象中同时引用了同一个对象数据,则在第一个JSON对象中会正常加载被引用的对象数据,但是在随后其他的JSON对象中则不会再次加载被引用的对象数据,而是通过"$ref"的方式指向第一个JSON对象中该对象数据的内存位置。

解决:两种方案,一是在一对多的情况下,在@onetomany的位置添加@JSONField(serialize = false) 注解,在此不详细介绍,具体参考下面的博文;二是关闭FastJson的循环引用检测特性,但是可能会导致stackoverflow,有两种方式。

(1)配置文件方式

<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteDateUseDateFormat</value>
                        <value>DisableCircularReferenceDetect</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

(2)参数方式

String jsonString = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonString);

 

参考:https://www.cnblogs.com/zhujiabin/p/6132951.html

           http://blog.csdn.net/weixiaodedao/article/details/51790790

posted on 2018-03-08 22:29  xieegai  阅读(603)  评论(0编辑  收藏  举报