一个方便的自定义注解,管理实体类

  一种比较方便的写法,但基于反射,效率比硬编码低。注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface ParamAnnotation {

    //是否必须参数
    public boolean isRequired() default false;

    //参数名称
    public String name();

    //对应接口中的参数名
    public String nameForInterface() default "";

    //源 JSON 中的 key
    public String nameForSource() default "";

}

  注解用于修饰实体类的属性。可标识是否必要属性、属性含义、调用接口时属性转为的 key 、由 JSON 初始化时属性对应的 key。

  配合基类方法使用:

public abstract class BaseParamBean implements Serializable {
    private static final long serialVersionUID = 6658268566L;

    /**
     * @Author Nxy
     * @Date 2020/4/2 10:37
     * @Param
     * @Return
     * @Exception
     * @Description 基于 ParamAnnotation 注解检查必填项是否完整
     */
    public boolean isComplete() throws IllegalAccessException, NoSuchFieldException {
        Class objClass = this.getClass();
        Class fatherClass = objClass.getSuperclass();
        Field fields[] = objClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ParamAnnotation.class)) {
                field.setAccessible(true);
                ParamAnnotation paramAnnotation = field.getAnnotation(ParamAnnotation.class);
                //如果是必填项
                if (paramAnnotation.isRequired()) {
                    Object value = field.get(this);
                    if (null == value) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * @Author Niuxy
     * @Date 2020/12/3 下午1:12
     * @Description 通过 JSON 及 nameForSource 属性的对应关系初始化
     */
    public void initFromJSON(JSONObject jsonObject) throws IllegalAccessException {
        if (jsonObject == null) throw new NullPointerException("jsonObject is null!");
        Class objClass = this.getClass();
        Field fields[] = objClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ParamAnnotation.class)) {
                field.setAccessible(true);
                ParamAnnotation paramAnnotation = field.getAnnotation(ParamAnnotation.class);
                String key = paramAnnotation.nameForSource();
                if (null == key || key.length() == 0) continue;
                if (!jsonObject.containsKey(key)) {
                    field.set(this, "");
                    continue;
                }
                field.set(this, jsonObject.get(key));
            }
        }
    }

    /**
     * @Author Niuxy
     * @Date 2020/12/3 下午1:11
     * @Description 根据 nameForInterface 属性获取 JSON
     */
    public JSONObject toJSON() throws IllegalAccessException {
        Class objClass = this.getClass();
        JSONObject jsonObject = new JSONObject();
        Field fields[] = objClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ParamAnnotation.class)) {
                field.setAccessible(true);
                ParamAnnotation paramAnnotation = field.getAnnotation(ParamAnnotation.class);
                String key = paramAnnotation.nameForInterface();
                jsonObject.put(key, field.get(this));
            }
        }
        return jsonObject;
    }
}

  可通过注解节省大量接口参数转化、数据初始化的代码,并使得实体类结构可读性更强(强制标明属性含义)。比如:

原文地址:https://www.cnblogs.com/niuyourou/p/14095158.html