Jsonlib 属性过滤器
/**
* @title JSON转换属性过滤器
* @description 用于JSON lib的JSON转换
* @author maohuidong
* @date 2017-04-06
*/
public class JsonConvertPropertyFilter implements PropertyFilter {
/**
* @function apply
* @param obj:待转换的对象 s: 对象的属性 value:对象的属性值
* @description JSON转换属性过滤器(Hibernate)
* @return true:过滤 false:不过滤
* @author maohuidong
* @date 2017-04-06
*/
@Override
public boolean apply(Object obj, String s, Object value) {
// hibernate代理对象未初始化,则过滤掉
if (value instanceof HibernateProxy) {
LazyInitializer initializer = ((HibernateProxy) value)
.getHibernateLazyInitializer();
if (initializer.isUninitialized()) {
return true;
}
}
// Hibernate持久化集合为初始化,则过滤掉(实体关联一对多)
if (value instanceof PersistentCollection) {
PersistentCollection collection = (PersistentCollection) value;
if (!collection.wasInitialized()) {
return true;
}
}
return false;
}
}
/**
* @function objStreamOutput
* @param 无
* @description 输出流封装
* @return 无
* @author maohuidong
* @date 2017-04-09
*/
protected void objStreamOutput(Object object,List<String> fieldFilter) throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
PrintWriter pWriter = response.getWriter();
//Json过滤
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
if(fieldFilter == null){
fieldFilter = new ArrayList<String>();
}
//Json转换过滤没用的字段
fieldFilter.add("hibernateLazyInitializer");
String[] filter = new String[fieldFilter.size()];
filter = fieldFilter.toArray(filter);
jsonConfig.setExcludes(filter);
//Json转换过滤未初始化的数据
jsonConfig.setJsonPropertyFilter(new JsonConvertPropertyFilter());
//结果转换为json字符串
String str = "";
//结果集能实例化为List
if(object instanceof List){
JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);
str = jsonArray.toString();
}else if(object instanceof String){
str = (String) object;
}else if(object instanceof Number){
str = object.toString();
}
else{
JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
str = jsonObject.toString();
}
pWriter.print(str);
pWriter.flush();
pWriter.close();
}
Jsonlib 属性过滤器的更多相关文章
- grootJs 属性过滤器
index10.html <html><head> <title>属性过滤器</title> <script src="jquery-1 ...
- fastjson格式化bean的简易属性过滤器
fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...
- FastJson前置属性过滤器
FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...
- HCNP Routing&Switching之BGP团体属性和团体属性过滤器
前文我们了解了BGP的路由过滤已经as-path过滤器的使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15542559.html:今天我们来聊一聊 ...
- jquery属性过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- jQuery表单对象属性过滤器再探究(原创)
上面例子的总结: 1.”+n+”千万不要把前面或者后面的+漏掉了.否则不会出现正确结果 2.$(“:checkbox”).click(countChecked)注意写法,不是click(functio ...
- 前端MVC Vue2学习总结(三)——模板语法、过滤器、计算属性、观察者、Class 与 Style 绑定
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器和 HTML 解 ...
- 第一章 JacksonUtil 序列化与反序列化属性总结
1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...
- Jquery最全过滤器总结
不管什么时候,总是有这么些时候:当我们使用jQuery的各种过滤器时,总是有那么几个记不牢,还要搜索一下或者翻翻手册!多少次想总结一下,最终都没总结,现在网上找到一篇总结的不错的,但是排版有点乱,本人 ...
随机推荐
- curl 知识点
curl :command line tool and library for transferring data with URLs curl 命令,常用缩写: curl 命令 缩写 说明 curl ...
- commonJS、AMD、es模块化 区别(表格比较)
commonJS.AMD.es6模块化 区别(表格比较): table th:first-of-type { } table th:nth-of-type(3) { width: 150px; } t ...
- git 知识点
git 删除远程已经推送过的文件或者文件夹 git rm -r --cached [文件或文件夹] git status git add . git commit -m '删除远程仓库文件,本地仓库和 ...
- 合并Dev BPL教程
一.准备工具 1.Devexpress vcl 14.2.2 下载地址http://download.csdn.net/user/rfjbco,共用个包,下载后解压,程序目录已带有DxAutoInst ...
- BASIC-30_蓝桥杯_阶乘计算
题目: 问题描述 输入一个正整数n,输出n!的值. 其中n!=1*2*3*…*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个大整数a,A ...
- bzoj1876 SuperGCD
Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要 ...
- man page ,info page 和/usr/share/doc/整理自鸟哥
- 比如查看date指令 - 命令:vbird@www ~]$ man date - 返回示例(部分):DATE(1) User Commands DATE(1) - 部分释义(注释2): ...
- 使用postman模拟appium的http请求
Appium是Server,接收http请求,使用Postman模拟请求 1.anyproxy 1.1.安装和运行 #安装 npm i -g anyproxy # 运行anyproxy,端口默认800 ...
- centos 安装LAMP环境后装phpmyadmin
首先在CentOS 上安装EPEL 要想安装EPEL,我们先要下载EPEL的rpm安装包. 1. 确认你的CentOS 的版本 首先通过以下命令确认你的CentOS 版本 $ cat /etc/Red ...
- nginx的日志分析
1.到NGINX把日志DOWN下来2.用命令cat xxxx.log | egrep '10/Jul/2015:01:[4-5]|2015-07-10 02:0[0-57]'>xxxx2.log ...