• 获取、设置注释的值


    示例1:通过反射获取@ApiModelProperty的value值

    
    	import io.swagger.annotations.ApiModelProperty;
    	import java.lang.reflect.Field;
    	
    	public class Main {
    	    public static void main(String[] args) throws Exception {
    	        // 获取目标类的Class对象
    	        Class<?> targetClass = User.class;
    	
    	        // 获取目标字段的Field对象
    	        Field field = targetClass.getDeclaredField("height");
    	
    	        // 获取ApiModelProperty注解
    	        ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
    	
    	        // 获取value属性的值
    	        String value = annotation.value();
    	
    	        // 打印value属性值
    	        System.out.println(value);
    	    }
    	}
    	
    	class User {
    	    @ApiModelProperty(value = "身高")
    	    private String height;
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    示例2:更新@ApiModelProperty的value值为"身高(cm)"

    
    import io.swagger.annotations.ApiModelProperty;
    import java.lang.reflect.Field;
    
        public static void main(String[] args) throws Exception {
            // 获取User类的Class对象
            Class<User> clazz = User.class;
    
            // 获取height字段
            Field field = clazz.getDeclaredField("height");
    
            // 获取字段上的ApiModelProperty注解
            ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
    
            // 获取注解的InvocationHandler
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(apiModelProperty);
    
            // 获取InvocationHandler中的memberValues字段
            Field annotationField = invocationHandler.getClass().getDeclaredField("memberValues");
    
            // 设置字段可访问
            annotationField.setAccessible(true);
    
            // 获取memberValues的值
            Map<String, Object> map = (Map<String, Object>) annotationField.get(invocationHandler);
    
            // 修改注解的value属性值
            map.put("value", "身高cm");
    
            // 打印修改后的注解值
            System.out.println(apiModelProperty.value());
        }
    
        @Data
        static class User {
            @ApiModelProperty(value = "身高")
            private String height;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    Dockerfile
    HTML基础学习第三课(列表的创建、有序和无序)
    17 参考书目
    英语新概念2-回译法-lesson10
    自动控制原理9.2---线性系统的可控性与可观测性(上)
    一位毕业生的自我分享
    Hyperledger Fabric搭建测试网络
    推荐一个C#开发的窗口扩展菜单,支持系统所以窗口
    2022 Nomachine 最简安装与使用指南
    Kubernetes PV与PVC 持久卷应用
  • 原文地址:https://blog.csdn.net/TM_enn/article/details/133745550