java filter 注解_通过注解实现JSONPropertyFilter

之前发表的文章涉及到JSONObject JSONArray属性过滤时,都是通过判断来实现的,那如果我们要在多处进行过滤的话,就会重复写很多代码,而且也不好修改。所以,这里我设计了一个注解annotation类,通过标注注解来实现JSONObject 和JSOnArray的属性过滤。

1、编写注解类

package com.renyiwei.wydns.json.annotation;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

*

* @author RenYiwei

*

*/

@Retention(RetentionPolicy.RUNTIME)

// 可以注解在字段,方法,类上

@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })

public @interface JSONPropertyFilter {

// 默认注解了就是过滤

boolean Value() default true;

}

2、编写AnnotationPropertyFilter实现PropertyFilter

package com.renyiwei.wydns.json;

import java.beans.IntrospectionException;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import net.sf.json.util.PropertyFilter;

import com.renyiwei.wydns.json.annotation.JSONPropertyFilter;

/**

*

* @author RenYiwei

*

*/

public class AnnotationPropertyFilter implements PropertyFilter {

public boolean apply(Object object, String name, Object value) {

// 通过读取注解来决定是否序列化

try {

// 获取类上的注解

JSONPropertyFilter clazzAnnotation = object.getClass().getAnnotation(JSONPropertyFilter.class);

// 如果类上有注解,并且值为true,表示需要过滤

if (clazzAnnotation != null && clazzAnnotation.Value()) {

// 不要这个字段 返回true

return true;

}

// 获取字段上的注解

Field field = object.getClass().getDeclaredField(name);

JSONPropertyFilter fieldAnnotation = field.getAnnotation(JSONPropertyFilter.class);

// 如果字段上注解了,并且值为true,表示需要过滤

if (null != fieldAnnotation && fieldAnnotation.Value()) {

// 不要这个字段 返回true

return true;

}

// 通过属性描述器 获取属性get方法的注解

PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());

Method getMethod = pd.getReadMethod();

if (null != getMethod) {

JSONPropertyFilter methodAnnotation = getMethod.getAnnotation(JSONPropertyFilter.class);

// 如果get方法上注解了,并且值为true,表示需要过滤

if (null != fieldAnnotation && fieldAnnotation.Value()) {

// 不要这个字段 返回true

return true;

}

}

} catch (NoSuchFieldException e) {

return false;

} catch (SecurityException e) {

return false;

} catch (IntrospectionException e) {

return false;

}

return false;

}

}

3、在javaBean上面标注注解

package com.renyiwei.wydns.domain;

import java.sql.Timestamp;

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

import javax.xml.bind.annotation.XmlTransient;

import com.renyiwei.wydns.json.annotation.JSONPropertyFilter;

public class Product {

private Long id;

// 在产品系统的产品编号

private Long productid;

// 在订单系统的产品编号

private Long orderpid;

private Long orderid;

private String name;

private String type;

private String extension;

// 在这里标注

@JSONPropertyFilter

private Server server;

// 创建时间

private String createtime;

// 到期时间

private String expdate;

// 购买时长

private String period;

// 在这里标注

@JSONPropertyFilter

private Client client;

private Timestamp ordertime;

// 省略get set方法

}

4、调用的时候传入AnnotationPropertyFilter对象

public String listAjax() {

List productList = productService.getAll();

Map returnMap = new HashMap();

JsonConfig config = new JsonConfig();

// 在这里传入AnnotationPropertyFilter对象

config.setJsonPropertyFilter(new AnnotationPropertyFilter());

returnMap.put("results", productList);

returnObj = JSONObject.fromObject(returnMap, config);

return SUCCESS;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值