Add the dependencies for swagger2 and swagger-ui in your pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
Add the annotation @EnableSwagger2
to your @SpringBootApplication
annotated main class and create a swagger Docket bean within this (or any other) configuration class.
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
This configuration will generate an API documentation over all spring controllers within your application. If you need to limit the API documentation generation to certain controllers you can choose between various RequestHandlerSelectors. E.g. you can generate your API documentation based on your package structure using RequestHandlerSelectors.basePackage("your.package.structure")
or based specific classes that you've annotated using RequestHandlerSelectors.withClassAnnotation(Api.class)
.
Use the annotations as described in the documentation, to enhance your controller classes and methods with additional information. To describe the general information of your api, like general title, description or the version, make use of the ApiInfoBuilder() within your Docket bean.
Example for the metadata definition using ApiInfoBuilder:
// Within your configuration class
public static ApiInfo metadata(){
return new ApiInfoBuilder()
.title("Your Title")
.description("Your Description")
.version("1.x")
.build();
}
// Within your method that definies the Docket bean...
docket.apiInfo(metadata());