The principle and application of Swagger2

Why Swagger2

1. 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.

2. Over time, constantly revised time synchronization interface must modify the interface documentation, and documentation and codes and in two different media, unless there is a strict management mechanism, otherwise easily lead to inconsistencies.

Swagger2 related dependent dependent

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>    <version>2.9.2</version>
</dependency>

swagger basic configuration


FIG @Configuration annotation on the representative class SwaggerConfig configuration classes, automatically loaded into the container after startup boot.

@ EnableSwagger2 is used to start Swagger support, it said it was a Spring Swagger profile.

Bean defines a method CustomDocket, Spring in the name is not important, important is that it returns a Docket class, DocumentationType.SWAGGER_2 as a parameter to the constructor Docket, specifies the swagger version used 2.0, apiInfo and after the call is next the apiInfo function to create the information Docket. apiInfo function takes build method ApiInfoBuilder ApiInfo class to create classes, build parameters of the method are added to the interface document it will be displayed in the Api swagger generated.

Configure access Swagger2-Api document page dependence of swagger-ui, rewrite addResourceHandlers method WebMvcConfigurer class, allows you to specify access to resources, access to documents generated after the Api configuration
---

swagger interface


After logging interface as follows:

---

Use swagger annotations


@Api: Controller generally used in an interface for the packet. (Eg: @Api (value = "User Interface", description = "user interface", tags = { "1.1.0"})


@ApiOperation: interface description, the method for the control layer controller. (Eg: @ApiOperation (value = "user query", notes = "Query The user ID information"))

@ApiParam: interface description, the method for the control layer controller parameters. (Eg: @ApiParam (name = "userId", value = "User ID", notes = "Query The user ID information"))


@ApiModelProperty: Entity Parameters, here I generated swagger comment by Mybatis-generate reverse engineering

The demo:

---

test results are successful, is expected to return to the parameter you want, more than for the spring boot binding swagger plug-in generates API interface documentation.

Guess you like

Origin www.cnblogs.com/smallZoro/p/11404824.html