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


注:在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


我们先来看一个例子:

package com.elong.bms;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class Test {
  public static void main(String[] args) {
  Map<String, Student> maps = new HashMap<String, Student>();
  Student s1 = new Student("s1", 16);

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

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

  System.out.println(new String(bytes));
  }
}

输出:

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

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

package com.elong.bms;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

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

public class Test {
  public static void main(String[] args) {
  Map<String, Student> maps = new HashMap<String, Student>();
  Student s1 = new Student("s1", 16);

  maps.put("s1", s1);
  maps.put("s2", s1);
  
  SerializerFeature feature = SerializerFeature.DisableCircularReferenceDetect;

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

  System.out.println(new String(bytes));
  }
}

输出如下:

{"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>



发布了121 篇原创文章 · 获赞 37 · 访问量 97万+
展开阅读全文

springmvc json返回死循环

06-27

错误描述:两个多对多的实体,他们没有关联的时候,返回没有问题,当关联后,返回第一个实体(已json返回),报死循环错误。 控制器代码: ``` // 输入框的模糊匹配 @ResponseBody @RequestMapping("/inputFuzzyQuery") public List<School> inputFuzzyQuery(String keyword) { List<School> schs = schoolService.fuzzyQueryMaxFive(keyword);//schs有数据 for (School sch : schs) { System.out.println(sch.toString()); } return schs; } ``` 错误信息 ``` at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23) at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23) at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23) at org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览