下载fastJSON jar   com.alibaba.fastjson

第一种:【写死的】

将需要序列化的字段传递进去,得到结果

//需要序列化的实体+字段
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Member.class,"字段1","字段2","可变字符串数组" );
String result = JSON.toJSONString(Member.class, filter);

第二种:【可以复用灵活】

Map保存类对象+此对象所有的字段

传进来需要阻隔的字段

package net.shopxx.ws.utils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.serializer.PropertyFilter; public class JSONExUtils implements PropertyFilter { //需要处理序列化阻隔的实体+实体所有的字段
private Map<Class<?>, String[]> excludes = new HashMap<Class<?>, String[]>(); /**
* apply 方法 返回true表示需要序列化
* 参数2 不需要序列化的字段【属性】
* 参数3 实体
*/
@Override
public boolean apply(Object object, String paramerter, Object entity) {
//对象为NULL 直接放行
if(entity == null){
return true;
} //获取需要序列化的 类对象
Class<?> clazz = entity.getClass(); //查找不需要序列化的字段
for (Map.Entry<Class<?>, String[]> exItem : this.excludes.entrySet()) {
// isAssignableFrom() 用来判断类型间是否有继承关系
if(exItem.getKey().isAssignableFrom(clazz)){
//不需要序列化的字段包含在所有字段中 下标>1 返回false
return -1 != Arrays.binarySearch(exItem.getValue(), paramerter);
}
}
return true;
} public void setExcludes(Class<?> cls, String...properties) {
excludes.put(cls, properties);
} public Map<Class<?>, String[]> getExcludes() {
return excludes;
} public void setExcludes(Map<Class<?>, String[]> excludes) {
this.excludes = excludes;
} //获取本对象所有的属性 暂时没用
public String[] just4Paramerters(Class<?> object){
Field[] fields = object.getDeclaredFields();
StringBuffer buffer = new StringBuffer();
for (Field field : fields) {
buffer.append(field.getName()+",");
}
if(buffer.length() > 0){
String[] paramerters = buffer.toString().split(",");
return paramerters;
}
return null;
} }

然后在需要使用的地方 调用即可!!

public void testName(){
JSONExUtils exUtils = new JSONExUtils();
exUtils.setExcludes(Member.class, new String[]{"需要阻隔的字段"});
String result = JSON.toJSONString("", exUtils);
}

【fastJSON】利用fastJSON处理循环引用的问题的更多相关文章

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

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

  2. 使用gc、objgraph干掉python内存泄露与循环引用!

    Python使用引用计数和垃圾回收来做内存管理,前面也写过一遍文章<Python内存优化>,介绍了在python中,如何profile内存使用情况,并做出相应的优化.本文介绍两个更致命的问 ...

  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. FastJson禁用循环引用检测

    我们先来看一个例子: package com.elong.bms; import java.io.OutputStream; import java.util.HashMap; import java ...

  6. 解决FastJson循环引用的问题

    temp 本来被循环引用,运行方法报错. 解决方法:对temp进行处理 SerializerFeature feature = SerializerFeature.DisableCircularRef ...

  7. 利用Fastjson注入Spring内存马

    此篇文章在于记录自己对spring内存马的实验研究 一.环境搭建 搭建漏洞环境,利用fastjson反序列化,通过JNDI下载恶意的class文件,触发恶意类的构造函数中代码,注入controller ...

  8. 【踩坑】利用fastjson反序列化需要默认构造函数

    利用 fastjson等 反序列化时需要注意,他可能会用到 默认的构造函数,如果没有默认构造函数,某些场景下可能会出现 反序列化熟悉为空的情况,如下图所示:

  9. Java基础/利用fastjson反序列化json为对象和对象数组

    利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...

  10. Java基础/利用fastjson序列化对象为JSON

    利用fastjson序列化对象为JSON 参考博客:http://blog.csdn.net/zeuskingzb/article/details/17468079 Step1:定义实体类 //用户类 ...

随机推荐

  1. 解析jsp的 tomcat 、resin

    一.tomcat 1. 安装JDK [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://www.lishi ...

  2. Nginx-1.6.3反向代理

    源码安装nginx cat /etc/redhat-release uname -rm yum install pcre-devel openssl-devel -y rpm -qa pcre pcr ...

  3. C++11空指针: nullptr

    参考[C++11]新特性--引入nullptr NULL 在C++中, 经常会用到空指针, 一般用NULL表示空指针, 但是NULL却是这样定义的 #ifndef NULL #ifdef __cplu ...

  4. 【python】r+,w+ 全局变量

    来源:http://www.educity.cn/wenda/352188.html r+:可读可写,若文件不存在,报错w+: 可读可写,若文件不存在,创建文本模式:遇换行符时根据操作系统不同自动转换 ...

  5. python_day1学习笔记

    一.Python 2.7.x 和 3.x 版本的区别小结 print函数 1.python2 import platform print ‘Python’, platform.python_versi ...

  6. 简述MapReduce计算框架原理

    1. MapReduce基本编程模型和框架 1.1 MapReduce抽象模型 大数据计算的核心思想是:分而治之.如下图所示.把大量的数据划分开来,分配给各个子任务来完成.再将结果合并到一起输出.注: ...

  7. python IDE的配置

    本人使用过的两款,系统环境ubuntukylin 15.04 jupyter 主要参考:ref1 和 ref2 遇到问题: error: [I 21:48:41.947 NotebookApp] Wr ...

  8. Pygame-依葫芦画瓢之兔獾大战

    Pygame-依葫芦画瓢之兔獾大战 前几天看到国外一个12岁的孩子写的兔獾大战游戏,心生敬佩,想当年我还是12岁的时候还不知电脑为何物,连小霸王都未曾玩过.自己也未曾想去搞游戏开发,纯属自娱自乐.在此 ...

  9. .net core 2.0学习记录(一):搭建一个.Net Core网站项目

    .Net Core开发可以使用Visual Studio 2017或者Visual Studio Code,下面使用Visual Studio 2017搭建一个.net Core MVC网站项目. 一 ...

  10. AtCoder Regular Contest 103 Problem D Robot Arms (构造)

    题目链接  Problem D 给定$n$个坐标,然后让你构造一个长度为$m$的序列, 然后给每个坐标规定一个长度为$m$的序列,ULRD中的一个,意思是走的方向, 每次从原点出发按照这个序列方向,每 ...