如何使用Springdoc隐藏OpenAPI文档中的端点

huangapple 未分类评论54阅读模式
标题翻译

How to hide endpoints from OpenAPI documentation with Springdoc

问题

Springdoc会自动生成所有处理程序方法的API文档,即使没有OpenAPI注解。

如何从API文档中隐藏端点?

英文翻译

Springdoc automatically generates a API documentation for all handler methods. Even if there are no OpenAPI annotations.

How can I hide endpoints from the API documentation?

答案1

得分: 26

@io.swagger.v3.oas.annotations.Hidden 注解可用于控制器的方法或类级别,用于隐藏一个或所有的端点。

(参见:https://springdoc.org/faq.html#how-can-i-hide-an-operation-or-a-controller-from-documentation)

示例:

  1. @Hidden // 隐藏所有端点
  2. @RestController
  3. @RequestMapping(path = "/test")
  4. public class TestController {
  5. private String test = "Test";
  6. @Operation(summary = "获取测试字符串", description = "返回一个测试字符串", tags = { "test" })
  7. @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "成功") })
  8. @GetMapping(value = "", produces = MediaType.TEXT_PLAIN_VALUE)
  9. public @ResponseBody String getTest() {
  10. return test;
  11. }
  12. @Hidden // 隐藏此端点
  13. @PutMapping(value = "", consumes = MediaType.TEXT_PLAIN_VALUE)
  14. @ResponseStatus(HttpStatus.OK)
  15. public void setTest(@RequestBody String test) {
  16. this.test = test;
  17. }
  18. }

编辑:

还可以仅为特定包的控制器生成API文档。

将以下内容添加到您的 application.properties 文件中:

  1. springdoc.packagesToScan=package1, package2

(参见:https://springdoc.org/faq.html#how-can-i-explicitly-set-which-packages-to-scan)

英文翻译

The @io.swagger.v3.oas.annotations.Hidden annotation can be used at the method or class level of a controller to hide one or all endpoints.

(See: https://springdoc.org/faq.html#how-can-i-hide-an-operation-or-a-controller-from-documentation)

Example:

  1. @Hidden // Hide all endpoints
  2. @RestController
  3. @RequestMapping(path = "/test")
  4. public class TestController {
  5. private String test = "Test";
  6. @Operation(summary = "Get test string", description = "Returns a test string", tags = { "test" })
  7. @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Success" ) })
  8. @GetMapping(value = "", produces = MediaType.TEXT_PLAIN_VALUE)
  9. public @ResponseBody String getTest() {
  10. return test;
  11. }
  12. @Hidden // Hide this endpoint
  13. @PutMapping(value = "", consumes = MediaType.TEXT_PLAIN_VALUE)
  14. @ResponseStatus(HttpStatus.OK)
  15. public void setTest(@RequestBody String test) {
  16. this.test = test;
  17. }
  18. }

Edit:

Its also possible to generate the API documentation only for controllers of specific packages.

Add following to your application.properties file:

  1. springdoc.packagesToScan=package1, package2

(See: https://springdoc.org/faq.html#how-can-i-explicitly-set-which-packages-to-scan)

答案2

得分: 2

以下是翻译好的内容:

也可以仅为特定路径生成 API 文档。

将以下内容添加到您的 application.yml 文件中:

  1. springdoc:
  2. paths-to-match: /api/**, /v1
英文翻译

Its also possible to generate the API doc only for specific Path.

Add following to your application.yml file:

  1. springdoc:
  2. paths-to-match: /api/**, /v1

答案3

得分: 1

  1. 如果您正在使用Swagger Api并且想要隐藏特定的端点则可以在该端点上使用 `@ApiOperation(value = "获取建筑", hidden=true)`...hidden 属性应为 true
  2. @RestController
  3. @Api(tags="建筑")
  4. @RequestMapping(value="/v2/buildings")
  5. public class BuildingsController {
  6. @ApiOperation(value = "获取建筑", hidden=true)
  7. @GetMapping(value = "/{reference}")
  8. public Account getBuildings(@PathVariable String reference) {
  9. ....
  10. }
  11. }
英文翻译

If you are working with Swagger Api and you want to hide specific endpoint then use @ApiOperation(value = "Get Building",hidden=true) on that endpoint...hidden attribute should be true.

  1. @RestController
  2. @Api(tags="Building")
  3. @RequestMapping(value="/v2/buildings")
  4. public class BuildingsController {
  5. @ApiOperation(value = "Get Building",hidden=true)
  6. @GetMapping(value = "/{reference}")
  7. public Account getBuildings(@PathVariable String reference) {
  8. ....
  9. }

huangapple
  • 本文由 发表于 2020年5月30日 20:12:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/62102261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定