Reputation: 635
Spring Boot RESTful web service & Swagger 2 here. I have the following @Configuration
class setup to configure Swagger for my service:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
I start my service and then I go to http://localhost:8080/v2/api-docs
(where Swagger is served from) and then paste that JSON into jsonlint.com and I see that Spring Boot is adding roughly ~40 endpoints automatically that I do not want Swagger to document. Things like the /trace
endpoint, and /health
, /env
and /beans
, etc. These are all things Spring Boot Actuator creates but that I don't want included in my public API documentation.
Is there a way to configure Swagger to not document these framework-level endpoints?
Upvotes: 2
Views: 9610
Reputation: 189
RequestHandlerSelectors.any()
will expose all the endpoints of your project.
On top of above answer, You can add use below code to restrict the exposure of API by providing your base package name in RequestHandlerSelectors.basePackage() method.
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.assingment.scalableweb"))
.paths(PathSelectors.any())
.build()
.apiInfo(metaData())
.tags(new Tag("DifferenceController", "Operations related to the Json Data"));
}
Upvotes: 5
Reputation: 1581
Using any()
will make documentation for your entire API available through Swagger. Use PathSelectors.ant()
to limit your endpoints.
Something like
.paths(PathSelectors.ant("/finance/**"))
will only display endpoints under /finance/
Upvotes: 4