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

被折叠的 条评论
为什么被折叠?



