我们先来看一个例子:

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>

转载地址:http://asialee.iteye.com/blog/2101915?utm_source=tuicool&utm_medium=referral

FastJson禁用循环引用检测的更多相关文章

  1. iOS之 FBMemoryProfiler FB的循环引用检测工具

    经过两天的google终于搞定了FBMemoryProfiler这个开源检测循环引用的工具.中间的曲折也是让人头疼,言归正传直接说一下这个memoryProfiler github:https://g ...

  2. 【fastJSON】利用fastJSON处理循环引用的问题

    下载fastJSON jar   com.alibaba.fastjson 第一种:[写死的] 将需要序列化的字段传递进去,得到结果 //需要序列化的实体+字段 SimplePropertyPreFi ...

  3. 【FastJSON】解决FastJson中“$ref 循环引用”的问题

    0.开发环境 SSH,EasyUI,MySQL 1.需求要求: (1)首先获取所有的贷款订单数据,即List <LoanOrder>. (2)然后从单个贷款订单实体LoanOrder去访问 ...

  4. Samples DataBind FastJson循环引用问题

    Fastjson full support databind, it's simple to use. Encode import com.alibaba.fastjson.JSON; Group g ...

  5. Atitit.json xml 序列化循环引用解决方案json

    Atitit.json xml 序列化循环引用解决方案json 1. 循环引用1 2. 序列化循环引用解决方法1 2.1. 自定义序列化器1 2.2. 排除策略1 2.3. 设置序列化层次,一般3级别 ...

  6. 前后台分离开发时遇到循环引用问题"$ref"

    1. 遇到的问题 { "errMsg": "", "data": { "baseinfo": { "freeT ...

  7. 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用

    学习 EF Code First+MVC 时遇到了在请求JsonResult时出现 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用 的异常,原因 ...

  8. 【读书笔记】iOS-ARC-Xcode检测循环引用

    一,在桌面上新建立一个工程,在ViewController.m中输入如下代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additio ...

  9. 序列化类型为“System.Reflection.Module”的对象时检测到循环引用

    在使用ajax调用web services时,正好返回的类型为datatable,想用通过json方式直接解析,但调用后,得到如下错误: 序列化类型为“System.Reflection.Module ...

随机推荐

  1. POJ 1840:Eqs 哈希求解五元方程

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14169   Accepted: 6972 Description ...

  2. python clickZan

    import pyautogui,time,random pyautogui.PAUSE = 3 pyautogui.FAILSAFE = True width, height = pyautogui ...

  3. 应用层上的协议HTTP

    HTTP http://www.runoob.com/http/http-tutorial.html https://www.cnblogs.com/houfee/articles/9161847.h ...

  4. java线程——线程局部变量

    一,线程局部变量ThreadLocal的作用 用于实现线程内部的数据共享,既对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,在另一个线程访问的时候,访问的由是另一份数据. 每个线程调用 ...

  5. 代码杂谈-or符号

    看到别人的代码里用了 or, 有点巧用. 记录一下. def func(a,b, context=None): # .... ctx = context or global_context() # . ...

  6. NiFi_Demo_调度示例

    1.背景 要求:每天凌晨1:00后开始每2min执行一次sql查询 2.作业编排 3.各模块配置 3.1 GenerateFlowFile 作用:用于产生flowfile,该flowfile内容为空. ...

  7. 数字证书原理(ssl,https)

    https://blog.csdn.net/qq_34115899/article/details/81298284 关于私钥公钥数字签名数字证书.https.RSA的一些讲解 http://www. ...

  8. ubuntu---【NVIDIA驱动 + CUDA 安装】不成功时的卸载方式

    NVIDIA驱动 与 CUDA 安装不成功时,可以卸载,检查相关问题(配置.兼容性等),重新安装.这里记录一下,卸载方式.

  9. ZooKeeper解决的问题

    1.解决分布式单点问题 https://www.jianshu.com/p/08b76bd7a634 2.实现分布式环境数据的一致性.访问ZooKeeper树结构时,不同节点返回的数据是一致,不会引起 ...

  10. c++ 获取GMT 时间和字符串

    需要跨平台,所以可选的只有std 和 boost: boost 比较复杂了 #include <boost/date_time/local_time/local_time.hpp> std ...