java 获取 属性的注释

Posted 正怒月神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 获取 属性的注释相关的知识,希望对你有一定的参考价值。

这次来个简单的方法

通过swagger2 的注解来生成

一,POM

<!-- https://mvnrepository.com/artifact/org.apache.poi/swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

二,模型

public class ReportShipmentDto implements Serializable 

    @ApiModelProperty(value="字段a")
    private String a;

    @ApiModelProperty(value="字段b")
    private String b;

    @ApiModelProperty(value="字段c",hidden = true)
    private String c;

    //get and set....

三,测试

@Test
    public void testGetModelAop() throws NoSuchFieldException 
        ReportShipmentDto rsDto=new ReportShipmentDto();
        Field[] fields=rsDto.getClass().getDeclaredFields();
        for (Field f : fields)
            ApiModelProperty anno = f.getAnnotation(ApiModelProperty.class);
            if(anno!=null && anno.hidden()!=true)
                System.out.println(anno.value());
        

    

以上是关于java 获取 属性的注释的主要内容,如果未能解决你的问题,请参考以下文章

Spring 注解

spring之注解构造IOC

Spring的注解配置与XML配置之间的比较

spring 注解是有啥作用

如何在Sql2008中获取表字段属性和注释?

如何在 Spring 中使用从属性文件获取的错误消息的注释验证?