# 忽略参数属性
WARNING
忽略参数属性是knife4j
提供的增强功能,开发者要想使用knife4j
提供的增强功能,必须在Swagger的配置文件中引入增强注解,各个版本的增强注解区别如下表:
软件 | 版本 | 增强注解 | 说明 |
---|---|---|---|
swagger-bootstrap-ui | <= 1.9.6 | @EnableSwaggerBootstrapUI | |
knife4j | <=2.0.0 | @EnableSwaggerBootstrapUi | |
knife4j | >=2.0.1 | @EnableKnife4j | 后续版本不会再更改 |
- 在使用
swagger-bootstrap-ui
的<=1.9.6
版本之前的代码方式,代码示例如下:
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
//more..
}
- 在使用
knife4j
的<=2.0.0
版本之前的代码方式,代码示例如下:
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUi
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
//more..
}
- 在使用
knife4j
的>=2.0.1
版本之后的代码方式,代码示例如下:
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
//more..
}
在1.9.5
版本,新增了在接口中可以忽略参数的新功能
通常我们在开发接口时,比如一个新增接口和一个修改接口,修改接口需要传递主键id、而新增接口则不需要传递此属性,但大部分情况,我们只写一个Model类,此时在新增接口时显示主键id会显得很多余.
使用自定义增强注解ApiOperationSupport
中的ignoreParameters
属性,可以强制忽略要显示的参数.
忽略的规则如下:
- 例如新增接口时,某实体类不需要显示Id,即可使用该属性对参数进行忽略.
ignoreParameters={"id"}
- 如果存在多个层次的参数过滤,则使用名称.属性的方式,例如
ignoreParameters={"uptModel.id","uptModel.uptPo.id"}
,其中uptModel是实体对象参数名称,id为其属性,uptPo为实体类,作为uptModel类的属性名称 - 如果参数层级只是一级的情况下,并且参数是实体类的情况下,不需要设置参数名称,直接给定属性值名称即可
在接口过滤时,主要有两种情况
# 一级参数
我们在使用实体类直接作为参数时,在我们的ui界面中是不会显示参数名称的,此时可以直接使用实体的属性名称进行参数忽略,例如如下代码:
@ApiOperation(value = "新增Model接口1")
@ApiOperationSupport(ignoreParameters = {"id","orderDate.id"})
@PostMapping("/insertMode1l")
public Rest<UptModel> insertModel1(UptModel uptModel){
Rest<UptModel> r =new Rest<>();
r.setData(uptModel);
return r;
}
实体类UptModel.java
文件代码
public class UptModel {
@ApiModelProperty(value = "主键id")
private String id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "邮箱")
private String email;
@ApiModelProperty(value = "订单信息")
private OrderDate orderDate;
}
此时,最终过过滤掉UptModel
的属性id和属性orderDate
类中的id属性,不在界面显示.
# JSON参数
如果请求参数是使用JSON的方式
代码如下:
@ApiOperation(value = "新增Model接口")
@ApiOperationSupport(ignoreParameters = {"uptModel.id","uptModel.name","uptModel.orderDate.id"})
@PostMapping("/insertModel")
public Rest<UptModel> insertModel(@RequestBody UptModel uptModel){
Rest<UptModel> r =new Rest<>();
r.setData(uptModel);
return r;
}
此时如果要过滤id的话,需要指定带上参数名称uptModel
最终忽略的值为ignoreParameters = {"uptModel.id","uptModel.name","uptModel.orderDate.id"}
被围观 人次