How to hide endpoints from OpenAPI documentation with Springdoc

瘦欲@ 提交于 2021-01-28 13:56:45

问题


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:


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:

@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;
    }

}

Edit:

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

Add following to your application.properties file:

springdoc.packagesToScan=package1, package2

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




回答2:


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.

@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) {
        ....
    }


来源:https://stackoverflow.com/questions/62102261/how-to-hide-endpoints-from-openapi-documentation-with-springdoc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!