Spring Boot Swagger2 used to quickly build RESTful API Documentation

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.xmladding 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.javacreation 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 @ApiOperationannotation API to add a description, by @ApiImplicitParams, @ApiImplicitParamannotation 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

Guess you like

Origin blog.csdn.net/abc_123456___/article/details/89187622