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 获取 属性的注释的主要内容,如果未能解决你的问题,请参考以下文章