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


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

    @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;
        }
    }

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

  • 相关阅读:
    MySQL主从.md
    mysqldump.md
    MySQL管理.md
    SQL语句.md
    如何在同一台服务器上部署两个tomcat
    loadrunner函数解密之web_reg_save_param
    loadrunner函数解密之web_reg_find
    Jmeter如何保持cookie,让所有请求都能用同一个cookie,免去提取JSESSIONID
    Jmeter如何提取响应头部的JSESSIONID
    Loadrunner如何进行有效的IP欺骗
  • 原文地址:https://www.cnblogs.com/niuyourou/p/14095158.html
Copyright © 2020-2023  润新知