java version of spring cloud + spring boot social e-commerce platform (ix) use Swagger2 build powerful RESTful API documentation (1)

Since the Spring Boot to rapidly develop, deploy and convenient features such as, I believe a large part of Spring Boot users will be used to build RESTful API. Moreover, the purpose are usually constructed RESTful API reasons due to multipath terminal, these terminals may share many of the underlying business logic, so we will abstract layer simultaneously serving a plurality of mobile Web or distal end.

As a result, our RESTful API is likely to face more than one developer or multiple development teams: IOS developers, Android developers or Web development. In order to reduce frequent communication with other teams usually costs during development, we will create a traditional practice RESTful API documentation to record all the details of the interface, however, this approach has several problems:

As many interfaces, and details of the complex (need to consider the different types of HTTP requests, HTTP headers, the HTTP request content, etc.), to create high-quality documents this in itself is a very difficult thing, grumbling downstream prevalent.
Over time, constantly revised interface must be synchronized at all times to modify the interface documents, and documents with two different codes but also in the media, unless there is a strict management mechanism, otherwise easily lead to inconsistencies.
In order to solve this problem above, this article will introduce heavy good partner RESTful API's 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:
Here Insert Picture Description

The following specifically describes the use of Swagger2 if Spring Boot. First, we need a Spring RESTful API project Boot to achieve, if you have not done this type of content, it is recommended to read

Add Swagger2 dependent
added in pom.xml 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
to create a configuration class Swagger2 Swagger2 in Application.java the same level.

@Configuration
@EnableSwagger2
public class Swagger2 {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.didispace.web"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
                .termsOfServiceUrl("http://blog.didispace.com/")
                .contact("程序猿DD")
                .version("1.0")
                .build();
    }
 
}

As shown in the code, by @Configuration annotation, to let Spring load the class configuration. Then to enable Swagger2 by @ EnableSwagger2 comment.

After re-create the Bean by createRestApi Docket function, apiInfo () is used to create the basic information of the Api (basic information exhibition now pages of the document). select () function returns a ApiSelectorBuilder example for controlling the interface which is exposed to Swagger to show, the present embodiment uses the specified package path to define scanning, scans all Swagger Controller API defined in this packet, and generates a document content (in addition to being @ ApiIgnore specified request).
JAVASpring Cloud needs of large enterprises distributed micro cloud services built B2B2C e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/95163142