springmvc中解决FastJson循环引用的问题

在springmvc中解决FastJson循环引用的问题

2015年06月16日 11:35:42 中下游国外我 阅读数:2320

 

注:在Spring4.1.6 MVC中配置如下即可禁止json转换时的引用功能:

 

配置文件pom.xml

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="features">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<bean id="DisableCircularReferenceDetect"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField"
value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"></property>
</bean>

 

 

参考文章:http://www.tuicool.com/articles/vUJBFn

 

 

我们先来看一个例子:

 
  1. package com.elong.bms;

  2.  
  3. import java.io.OutputStream;

  4. import java.util.HashMap;

  5. import java.util.Map;

  6.  
  7. import com.alibaba.fastjson.JSON;

  8.  
  9. public class Test {

  10. public static void main(String[] args) {

  11. Map<String, Student> maps = new HashMap<String, Student>();

  12. Student s1 = new Student("s1", 16);

  13.  
  14. maps.put("s1", s1);

  15. maps.put("s2", s1);

  16.  
  17. byte[] bytes = JSON.toJSONBytes(maps);

  18.  
  19. System.out.println(new String(bytes));

  20. }

  21. }

输出:

{"s1":{"age":16,"name":"s1"},"s2":{"$ref":"$.s1"}}

可以看到,这个json如果发到前端是无法使用的,幸好FastJson提供了解决办法,我们来看下,解决办法为禁用循环引用检测,代码如下:

 
  1. package com.elong.bms;

  2.  
  3. import java.io.OutputStream;

  4. import java.util.HashMap;

  5. import java.util.Map;

  6.  
  7. import com.alibaba.fastjson.JSON;

  8. import com.alibaba.fastjson.serializer.SerializerFeature;

  9.  
  10. public class Test {

  11. public static void main(String[] args) {

  12. Map<String, Student> maps = new HashMap<String, Student>();

  13. Student s1 = new Student("s1", 16);

  14.  
  15. maps.put("s1", s1);

  16. maps.put("s2", s1);

  17.  
  18. SerializerFeature feature = SerializerFeature.DisableCircularReferenceDetect;

  19.  
  20. byte[] bytes = JSON.toJSONBytes(maps,feature);

  21.  
  22. System.out.println(new String(bytes));

  23. }

  24. }

输出如下:

{"s1":{"age":16,"name":"s1"},"s2":{"age":16,"name":"s1"}}

问题是如果我们在spring mvc中使用的时候,需要将

SerializerFeature注入到MessageConverter里面,

 

FastJsonHttpMessageConverter

但是 SerializerFeature是一个enum类型的,又是一个array,考虑到大部分人对这个不熟悉,直接上代码了。

   

<bean id="jsonConverter"
    class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
    <property name="features">
      <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
        <value>DisableCircularReferenceDetect</value>
      </array>
    </property>
  </bean>
  <bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
    <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"></property>
  </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值