2

Springdoc 自动为所有处理程序方法生成 API 文档。即使没有 OpenAPI 注释。

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

4

2 回答 2

8

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

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

例子:

@Hidden // Hide all endpoints
@RestController
@RequestMapping(path = "/test")
public class TestController {

    private String test = "Test";

    @Operation(summary = "Get test string", description = "Returns a test string", tags = { "test" })
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Success" ) })
    @GetMapping(value = "", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody String getTest() {
        return test;
    }

    @Hidden // Hide this endpoint
    @PutMapping(value = "", consumes = MediaType.TEXT_PLAIN_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public void setTest(@RequestBody String test) {
        this.test = test;
    }

}

编辑:

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

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

springdoc.packagesToScan=package1, package2

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

于 2020-05-30T12:12:50.163 回答
1

如果您正在使用 Swagger Api 并且想要隐藏特定端点,则 @ApiOperation(value = "Get Building",hidden=true)在该端点上使用...hidden 属性应该为 true。

@RestController
@Api(tags="Building")
@RequestMapping(value="/v2/buildings")
public class BuildingsController {

    @ApiOperation(value = "Get Building",hidden=true)
    @GetMapping(value = "/{reference}")
    public Account getBuildings(@PathVariable String reference) {
        ....
    }
于 2020-05-31T02:11:50.460 回答