java 获取 属性的注释(二)

这篇博客介绍了如何通过Swagger2的注解轻松生成API文档。首先,添加了Swagger2和Swagger-Bootstrap-UI的依赖到POM文件中。接着,展示了如何为一个名为ReportShipmentDto的模型类添加ApiModelProperty注解,包括隐藏某个字段。最后,提供了一个测试用例,展示如何遍历类的字段并打印非隐藏的ApiModelProperty注解值。

这次来个简单的方法

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

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值