Search code examples
javaspring-bootopenapispringdoc

How to hide endpoints from OpenAPI documentation with Springdoc


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?


Solution

  • 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:

    It's 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)