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

时间:2020-05-30 12:12:50

标签: java spring-boot openapi springdoc

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

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

2 个答案:

答案 0 :(得分:4)

@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

答案 1 :(得分:0)

如果您正在使用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) {
        ....
    }