Spring Boot e-commerce project 25: commodity classification module 4: use Swagger to automatically generate API documents; (PS: the @ Configuration annotation is used again on the Configuration class)

Posted by solarisuser on Thu, 24 Feb 2022 07:19:46 +0100

explain:

(1) Swagger's main function is to [generate API documents]; Moreover, swagger will update in real time when our project is updated;

catalogue

1: Introduce and configure Swagger;  

1. In POM XML, introduce the dependencies required by Swagger;

2. At the program entrance MallApplication, use [@ EnableSwagger2 annotation]: open Swagger;

3. Create SpringFoxConfig class to configure Swagger;

4. Create ImoocMallWebMvcConfig class to configure address mapping;

2: Use Swagger;

1. Access Swagger document address; Then, the basic information of the interface can be described through [@ ApiOperation("interface description information")];

2. You can debug the interface on the Swagger document page; (therefore, this Swagger document page can be used as a postman)

1: Introduce and configure Swagger;  

1. In POM XML, introduce the dependencies required by Swagger;

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

explain:

(1) Dependency description;  

2. At the program entrance MallApplication, use [@ EnableSwagger2 annotation]: open Swagger;

3. Create SpringFoxConfig class to configure Swagger;

SpringFoxConfig class:

package com.imooc.mall.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;

@Configuration
public class SpringFoxConfig {

    //visit http://localhost:8083/swagger-ui.html, you can see the API document
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Admire fresh")
                .description("")
                .termsOfServiceUrl("")
                .build();
    }
}

explain:

(1) This configuration file generally does not need to be written by ourselves; It is a template code. When we need it in the future, just come to copy directly, and then fine tune it according to our needs;

4. Create ImoocMallWebMvcConfig class to configure address mapping;

package com.imooc.mall.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description: configure address mapping
 */
@Configuration
public class ImoocMallWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

explain:

(1) The @ Configuration annotation is used here again to illustrate that this class is a Configuration class;;;; The first time I encountered [@ Configuration annotation] was in[ Spring IoC container and bean management 25: use Java Config to implement Spring IoC 1: object instantiation; (@Configuration,@Bean)];

(2) Configuration content analysis;

(3) There is no need to remember the content here. Just come and copy it when necessary;

2: Use Swagger;

1. Access Swagger document address; Then, the basic information of the interface can be described through [@ ApiOperation("interface description information")];

Start the project; Then visit the configuration in spring foxconfig[ http://localhost:8083/swagger-ui.html ]This address;

You can see that all the URLs in the project and the exposed interfaces are collected here; In the future, if we want to interact with other people, we will provide it directly[ http://localhost:8083/swagger-ui.html ]With this address, the other party can know which interfaces we have;

On the interface, we can use [@ ApiOperation("interface description information")] to describe the basic information of the interface;

Restart the project and refresh the page:

2. You can debug the interface on the Swagger document page; (therefore, this Swagger document page can be used as a postman)

............................................................ 

Topics: Spring Boot Back-end