编程语言
首页 > 编程语言> > java 获取 属性的注释(二)

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());
        }
 
    }

 

标签:ApiModelProperty,ReportShipmentDto,java,String,value,注释,private,anno,属性
来源: https://www.cnblogs.com/hanjun0612/p/15919796.html