简体   繁体   English

如何在 Swagger 中将 RequestParam 标记为可选?

[英]How Do I Mark A RequestParam As Optional In Swagger?

I am generating the swagger docs for my REST API using SpringFox.我正在使用 SpringFox 为我的 REST API 生成 swagger 文档。

I have added an optional parameter to my API now:我现在向我的 API 添加了一个可选参数:

@ApiOperation(
  value = "Get all cars"
)
@GetMapping(value = "/cars", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO<CarDTO>> getCars(
  @RequestParam(defaultValue = "1") Integer page,
  @RequestParam(required = false) String status) {
  ResponseDTO<CarDTO> response = service.getCars(page, status);
  return ResponseEntity.ok(response);
}

How do I highlight in the swagger docs that one is required and the other is optional?如何在 swagger 文档中突出显示一个是必需的,另一个是可选的?

You have the @ApiParam annotation you can use, it has a property required which you can put to true or false depending on your needs您有可以使用的@ApiParam注释,它有一个required属性,您可以根据需要将其设置为truefalse

@ApiOperation(
  value = "Get all cars"
)
@GetMapping(value = "/cars", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO<CarDTO>> getCars(
  @ApiParam(required = true) @RequestParam(defaultValue = "1") Integer page,
  @ApiParam(required = false) @RequestParam(required = false) String status) {
  ResponseDTO<CarDTO> response = service.getCars(page, status);
  return ResponseEntity.ok(response);
}

As you can see in the documentation , it has other properties like正如您在文档中看到的那样,它具有其他属性,例如

  • access
  • allowableValues
  • allowMultiple
  • defaultValue
  • name
  • value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM