Springboot integrates Swagger to automatically generate interface documents

Why use Swagger to automatically generate interface documents

Swagger is a complete framework. In the project, it is used to automatically generate the corresponding interface documents, and can directly test the interface on the visualization tool. I will sort out swagger2 0 + version and swagger3 Differences and deployment methods of version 0

1. Project integration Swagger

The premise of this integration is that you can use Swagger to integrate with the spring Maven, which is based on the following two methods

1.1 Maven integrated Swagger

Code slice in pom file



1.2 Gradle integrated Swagger

Code slice in Gradle file

    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

2. Start configuring Swagger

After importing the required jar packages through the above two methods, we can start to configure Swagger. First, we configure a SwaggerConfig class to start Swagger

1. Create a swaggerconfig java

SwaggerConfig.java code slice

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;

 * @description: Swagger2 to configure
 * @date: 2020-11-19 12:25  
//Represents a configuration class
//Turn on Swagger
public class SwaggerConfig implements WebMvcConfigurer {
	//Because Swagger automatically generates documents by invading code, a switch can be set here to
	//Turn off swagger. On the production environment Enabled = false to close
    private boolean swaggerabled;
    private String title;
    private String description;
    private String version;

    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2) //Swagger version: if the imported swagger dependency is 3.0, the Type here is OAS_ thirty

    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
        		//Title of Api document
                //Description of Api document
                //Version number of Api document

3. Handle the entity class User of the input parameter

The code slice in the User file

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import java.util.Date;

 * @description:
 * @author: 
 * @date: 
@ApiModel( description= "Requested user entity class")
public class User {
    protected String id;
    /** User number [32,0] Not NULL */
	@ApiModelProperty(hidden = true)
    protected String department;

Add @ ApiModel annotation to the entity class. Description is the description of the entity
You can see that in the attribute, we use the @ ApiModelProperty annotation and the attribute hidden=true, so we can't see the attribute in the request body in the automatically generated interface document

In this way, we have completed the first step of configuring Swagger. Next, we process the Controller that needs to automatically generate interface documents

4. Processing Controller

Code slice in Controller

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

 * @description:
 * @author: 
@Api(tags = "Swagger test")
public class UserController {

    @ApiOperation(value = "Get user entity class",notes = "according to ID Get user entity class")
    @ApiImplicitParam(name = "user",value = "User entity class",dataType = "",required = true)
    public User getUserById(@RequestBody User user)
        return userService.getUserById(user);

Adding the @ Api annotation to the Controller above means that a module can be understood as a method in the interface document @ Api, which will not be introduced here

On the getUserById method in UserController, we can see some corresponding relationships between some annotations of Swagger and the automatically generated interface document

5. Modify the Springboot startup class

We need to add the annotation of Swagger in the Application of Springboot startup class

Code slice in Application file

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);

6. Access SwaggerUi automatically generated by annotation

After adding the @ EnableSwagger2 annotation in Applicaiton above, we start the Spriingboot project. Remember to add the springboot web dependency first to make it a web project. After starting, we visit the address
http://localhost:8080/swagger-ui.html is enough. Here I start the springboot server locally Port is the default 8080
The effect is as follows

7. Update the second interface later

The interface of automatically generating documents displayed in the second interface will be updated later

Tags: Java Spring Boot swagger swagger2

Posted by msk_1980 on Sat, 07 May 2022 09:50:04 +0300