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

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

复制代码
@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;
    }
}
复制代码

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

posted @   牛有肉  阅读(516)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示