SpringFox - 隐藏 Swagger-ui 中的某些字段,这些字段对于端点调用不是 required
SpringFox - Hide certain fields in Swagger-ui that aren't required for the call to an endpoint
我想知道是否有任何方法可以让 SpringFox 不显示特定实体的所有字段,这些字段在对特定端点的调用中不是 required。
例如:
具有以下实体:
public class Car {
long id;
String name;
int wheels;
String type;
boolean canFly;
}
以及以下端点:
@RequestMapping(method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car get(@RequestParam(value = "carId", required = true) long projectId) {
return carService.get(carId);
}
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car create(@RequestBody Car car) {
return carService.create(car);
}
@RequestMapping(method = RequestMethod.PUT,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car update(@RequestBody Car car) {
return carService.update(car);
}
问题是,在创建汽车端点中,只有名称和车轮是 required,但在文档中 Swagger-ui 显示所有字段,就好像它们是 requi红色。我已经尝试过 @JsonViews
但 Springfox 还没有处理它们。
有什么办法可以避免这种情况吗?
使用@ApiModelProperty
(来自io.swagger.annotations
)
- 使用
required
定义 属性 是强制性的还是可选的。
- 使用
hidden
,您可以在 Swagger UI 中隐藏 属性,但是如果设置了它,它无论如何都会返回。
例如:
public class Car {
@ApiModelProperty(value = "id", required = true)
long id;
@ApiModelProperty(value = "wheels", required = true)
int wheels;
@ApiModelProperty(value = "name", hidden = true)
String name;
@ApiModelProperty(value = "type", hidden = true)
String type;
@ApiModelProperty(value = "canFly", hidden = true)
boolean canFly;
}
由于您对请求和响应使用相同的模型(在上面的示例中),GET 端点文档中的属性也将被隐藏(记住这一点)。如果您不想要这种行为,请使用单独的模型。
从springfox-boot-starter 3.0.0开始
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
您可以通过在您需要的字段上添加 Jackson JsonProperty 注释来非常轻松高效地完成此操作。
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
例如你有一个模型 class 客户,你想要两者:
- 在请求的 swagger 中隐藏字段 ID(例如 POST 和 PUT)但在响应中显示它
- 不接受控制器中的字段 ID(这样无论调用者设置什么,它的值始终为 0)但 return 响应中客户端的 ID 值。
您可以通过在 id 字段上添加注释来实现这些
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class Client {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private long id;
private String name;
}
我想知道是否有任何方法可以让 SpringFox 不显示特定实体的所有字段,这些字段在对特定端点的调用中不是 required。
例如:
具有以下实体:
public class Car {
long id;
String name;
int wheels;
String type;
boolean canFly;
}
以及以下端点:
@RequestMapping(method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car get(@RequestParam(value = "carId", required = true) long projectId) {
return carService.get(carId);
}
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car create(@RequestBody Car car) {
return carService.create(car);
}
@RequestMapping(method = RequestMethod.PUT,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car update(@RequestBody Car car) {
return carService.update(car);
}
问题是,在创建汽车端点中,只有名称和车轮是 required,但在文档中 Swagger-ui 显示所有字段,就好像它们是 requi红色。我已经尝试过 @JsonViews
但 Springfox 还没有处理它们。
有什么办法可以避免这种情况吗?
使用@ApiModelProperty
(来自io.swagger.annotations
)
- 使用
required
定义 属性 是强制性的还是可选的。 - 使用
hidden
,您可以在 Swagger UI 中隐藏 属性,但是如果设置了它,它无论如何都会返回。
例如:
public class Car {
@ApiModelProperty(value = "id", required = true)
long id;
@ApiModelProperty(value = "wheels", required = true)
int wheels;
@ApiModelProperty(value = "name", hidden = true)
String name;
@ApiModelProperty(value = "type", hidden = true)
String type;
@ApiModelProperty(value = "canFly", hidden = true)
boolean canFly;
}
由于您对请求和响应使用相同的模型(在上面的示例中),GET 端点文档中的属性也将被隐藏(记住这一点)。如果您不想要这种行为,请使用单独的模型。
从springfox-boot-starter 3.0.0开始
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
您可以通过在您需要的字段上添加 Jackson JsonProperty 注释来非常轻松高效地完成此操作。
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
例如你有一个模型 class 客户,你想要两者:
- 在请求的 swagger 中隐藏字段 ID(例如 POST 和 PUT)但在响应中显示它
- 不接受控制器中的字段 ID(这样无论调用者设置什么,它的值始终为 0)但 return 响应中客户端的 ID 值。
您可以通过在 id 字段上添加注释来实现这些
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class Client {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private long id;
private String name;
}