我在Springfox和Spring Boot中使用Swagger2。我有一个如此定义的端点:
@ApiOperation(value = "save", nickname = "Save Store")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 500, message = "Failure", response = ErrorResource.class)})
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public void save(@Valid @RequestBody Store store, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException("Invalid Store", bindingResult);
}
this.storeService.save(store);
response.setHeader("Location", request.getRequestURL().append("/").append(store.getId()).toString());
}
生成的API文档在模型架构中显示id
Store
。从技术上讲,在创建Store
时,JSON不应包含id
。我试图找出如何告诉Swagger / Springfox忽略id
,但仅限于此端点。
答案 0 :(得分:0)
您可以通过使用 @ApiModelProperty 注释类的属性并将其隐藏属性设置为true来隐藏模型中的字段。
import io.swagger.annotations.ApiModelProperty;
public class Store {
@ApiModelProperty(hidden = true)
private Long id;
}
不幸的是,通过这样做,您将在使用Store类作为输入的每个端点上隐藏id字段。显示另一个端点的字段需要一个单独的类。