Construction and Use of SWAGGER

Keywords: Spring SpringBoot JSON Attribute

Construction of SWAGGER and its integration with SPRINGBOOT

1. Construction of swagger official api demo

1. swagger ui github download address: https://github.com/swagger-api/swagger-ui

2. Install nodejs: Download address: https://nodejs.org/en/download/ (Just a little at a time)

3. Install Express Dependency (Express is a concise and flexible node.js Web application framework that provides a series of powerful features to help you create a variety of Web applications)

The command steps are as follows: MKDIR node_app_swagger (create empty directory) CD node_app_swagger (enter the directory) npm init initialization command to generate package.json file npm install express --save installation express (-- write dependency information into package.json file)

4. Build express web project and start it

Create a new index.js file in the project root directory and copy the following code to the file

var express = require('express');
var app = express();
app.use('/static', express.static('public'));
app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Then create a new public folder in the project root directory, and copy all the files in the dist directory of the downloaded Swagger UI file to the public folder.

Finally start command: node index.js open browser input http://localhost:3000/static/index.html You can see the effect.

The above swagger official api demo is completed.

In addition, if you need to customize the content of the swagger ui interface, you can export the custom swagger.json file (user interface configuration file) through the swagger editor editing.

2. SwaggerUI+SpringBoot

1. Using springboot Online Tools to Quickly Build a springboot maven Project https://start.spring.io/

(ps: Spring boot projects constructed quickly by this method need to add spring-boot-starter-web dependencies, which have been added to the underlying dependencies and can be directly replicated. Otherwise, the integrated swagger startup project has been reporting errors, and the author has stepped on a big pit here.)

2. Introducing swagger dependencies into pom files

        <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>    

    <!--Use Swagger2 structure RESTful API File-->
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.8.0</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.8.0</version>
	</dependency>

3. Configure swagger config class

package com.cch.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * Here's how to use swagger2 to generate api documents
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //Current package path
                .apis(RequestHandlerSelectors.basePackage("com.cch.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * Building detailed information functions for api documents
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //Page title
                .title("Title")
                //describe
                .description("describe")
                .termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
                //Founder
//                .contact("api")
                //Edition
                .version("1.0")
                .build();
    }
}

4. Use the swagger annotation controller to generate the corresponding API online documents (not written, but not recommended, the swagger framework scans the package and generates the default document description in English)

5. Start the spring boot project, visit http://localhost:8080/swagger-ui.html You can see the swagger online api document ui interface

6. The usage of swagger's annotations (red is the common annotation)

 

Api annotation Action description Use location
@Api Controller Description For the controller class
@ApiOperation Method description Used in controller's method
@ApiImplicitParams Non-object parameter set Used in controller's method
@ApiImplicitParam Non-object parameter description Used in the @ApiImplicitParams method
@ApiResponses Response set Used in controller's method
@ApiResponse Response parameter description Used in @ApiResponses
@ApiModel Describe the meaning of the returned object For returning object classes
@ApiModelProperty Object attribute Used on fields of access parameter objects

Attribute details in @ApiImplicitParam

attribute Value Use location
paramType - Query parameter type
- path Submit data in the form of address
- query Automatic mapping assignment directly with parameters
- body POST-only support for stream submission
- header Parameters are submitted in request headers
- form Submitting in form form only supports POST
dataType - The data type of the parameter is only described as a flag, and it is not validated in practice.
- Long -
- String -
- int -
- Object -
name - Receiving parameter name (nonparametric description)
value - Meaning Description of Receiving Parameters
required - Is the parameter mandatory
- true Must fill
- false Non essential
defaultValue   Default value

Posted by MaTT on Sat, 04 May 2019 15:50:38 -0700