Swagger2, it can be easily integrated into Spring Boot in, and with Spring MVC program with a strong organization RESTful API documentation. It can reduce the amount of work we created the document, while the description content and integrated into implementation code, modify the code so that maintenance documentation and integrated into one, allows us to easily modify documentation at the same time to modify the code logic. In addition Swagger2 page also provides a powerful test capabilities to debug each RESTful API. Specific results as shown below:
In pom.xml
adding dependent Swagger2
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Creating Swagger2 configuration class
In the Application.java
creation Swagger2 configuration class at the same level Swagger2
. Note basepackage package name should be changed in your own package name, it can be scanned to
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lyb.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:linyongbin")
.contact("linyongbin")
.version("1.0")
.build();
}
}
We @ApiOperation
annotation API to add a description, by @ApiImplicitParams
, @ApiImplicitParam
annotation parameters to add a description.
Start Spring Boot program, visit: HTTP: // localhost: 8080 / Swagger-ui.html
will be able to see the page previously displayed RESTful API, we also can be tested in the face of our interface