fastjson格式化bean的简易属性过滤器
fastjson的bean属性过滤器
有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简。下面直接看代码
package com.base.config; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; public class Student {
private int id;
private String name;
private String addr; public Student(int id, String name, String addr) {
super();
this.id = id;
this.name = name;
this.addr = addr;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
} public static void main(String[] args) {
Student s = new Student(1, "test", "银川市");
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","name");//只输出需要的属性
SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter(Student.class, "id","addr");//只输出需要的属性
System.out.println("filter \n"+JSONObject.toJSONString(s,filter));
System.out.println("------------------------------------");
System.out.println("filter2 \n"+JSONObject.toJSONString(s,filter2));
}
}
filter
{"id":1,"name":"test"}
------------------------------------
filter2
{"addr":"银川市","id":1}
这样能最大程度简化我们需要的bean属性,来减少不必要的数据量,提示响应速度。
也许json属性的输出序列不是你想要的[默认属性以字典序列排序],大部分时候序列并不是很重要,重要的是数据。
fastjson格式化bean的简易属性过滤器的更多相关文章
- FastJson前置属性过滤器
FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...
- fastjson过滤不需要的属性
以下是一个通用的对象转json的方法,使用的fastjson的SimplePropertyPreFilter 对象,个人感觉比使用PropertyPreFilter的匿名内部类形式的过滤器更好用!直接 ...
- FastJson序列化时过滤字段(属性)的方法总结
FastJson序列化时(即转成JSON字符串时),可以过滤掉部分字段,或者只保留部分字段,方法有很多,下面举一些常用的方法. 方法一.FastJson的注解 @JSONField(serialize ...
- grootJs 属性过滤器
index10.html <html><head> <title>属性过滤器</title> <script src="jquery-1 ...
- spring 的配置 bean>>property>>name属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- spring中bean的scope属性理解
bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- CSS2.1SPEC:视觉格式化模型之width属性详解(下)
本文承接CSS2.1SPEC:视觉格式化模型之width属性详解(上),继续分析CSS视觉格式化模型中width以及相关值的计算问题: 注:与上节不同,本节的demo中由于出现了float,absol ...
随机推荐
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [Linux] Linux进程PID散列表
linux系统中每个进程由一个进程id标识,在内核中对应一个task_struct结构的进程描述符,系统中所有进程的task_struct通过链表链接在一起,在内核中,经常需要通过进程id来获取进程描 ...
- 信与信封问题(codevs 1222)
题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封.不幸的是,Small Joh ...
- linux架构图
/ 根目录 │ ├boot/ 启动文件.所有与系统启动有关的文件都保存在这里 │ └grub/ Grub引导器相关的文件 │ ├dev/ 设备文件 ├proc/ 内核与进程镜像 │ ├mnt/ 临时挂 ...
- 如何 ︰ 执行批量更新和插入使用.NET 提供程序在 C#.NET OpenXML
https://support.microsoft.com/zh-cn/kb/315968 如何 ︰ 执行批量更新和插入使用.NET 提供程序在 C#.NET OpenXML Email Prin ...
- Wcf for wp8 连接数据库,读写数据库,显示数据库数据(二)
下载: Microsoft® SQL Server® 2012 Express http://www.microsoft.com/zh-cn/download/details.aspx?id=2906 ...
- https_request
<?php $access_token = ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_se ...
- JNDI 是什么
转自:http://blog.csdn.net/zhaosg198312/article/details/3979435 JNDI是 Java 命名与目录接口(Java Naming and Dire ...
- ECJTU大一暑假集训
第二场比赛:一签到题没做出来!!!死活不远做下去了,开始发狂,最后还有2个半小时开始做别的,陆续A了几道: 我还能怪谁呢,我渣,我傻逼,就这样!! 7/19:早就想自己建一个博客了,也就是一直想想没 ...
- loj 1011(状态压缩+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...