Maven Dependencies
Add the two dependencies below to use swagger and enable the swagger ui.<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
Spring Integration
With Spring annotation driven configuration this process is easy. Create a class and annotate it with @Configuration so that spring uses it. Additionally use @EnableSwagger2 to make sure that swagger is enabled.@Configuration
@EnableSwagger2
public class SwaggerConfiguration
{
/**
* Add to the swagger documentation
*
* @return The {@link Docket} class with api information and config
*/
@Bean
public Docket getApiDocumentation()
{
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Project ReST Service")
.apiInfo(new ApiInfoBuilder()
.title("My Service")
.description("My service which does lots of interesting things.")
.build())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
With spring boot the situation isn't really any different to that above accept that the @EnableSwagger2 can be put on the same class as a the @SpringBootApplication annotation or left exactly as it is above.
Swagger Annotations
In addition to the annotation to enable swagger (@EnableSwagger2) the other main annotations to use are,
@Api - this can be used on a controller to describe the overall behaviour
@ApiOperation - put this on the methods in the controller to describe what they do
@ApiParam - used to describe the particular parameter that is passed to a controller method
A full list of annotations can be found here https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X
No comments:
Post a Comment