“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. Configuration Swagger

1.1 Configuring Swagger- UI Basic Information

There’s no set method, so you have to use the constructor to set it, but there’s usually very little that you need to set, so it’s all fixed

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(a){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo();
    }
    // Configure Swagger information ==>ApiInfo
    public ApiInfo apiInfo(a){
        // Author information
        Contact contact=new Contact("Yang Mingyu"."http://blog.ymy12138.cn"."[email protected]");
        return new ApiInfo("Api Documentation"./ / title
                "Api Documentation".// Detailed description
                "1.0"./ / version
                "urn:tos".// Developer information, can be a website
                contact,// Author information
                "Apache 2.0"./ / rely on
                "http://www.apache.org/licenses/LICENSE-2.0".newArrayList()); }}Copy the code

1.2 Configuring Swagger Scanning Interfaces

That is, configure the scan Controller and the corresponding return value

Use chain notation to pass a Docket as an argument when you create it


return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .enable(true);
                .apis(RequestHandlerSelectors.xxx())
                .paths(PathSelectors.yyy());
                .build();
//apis, which to scan
//xxx
//1. BasePackage: specifies the package to be scanned
//2. any() : scans all
//3. none() : does not scan
//4. WithClassAnnotation: Scan the annotation on the class, the annotation's reflection object
//5. WithMethodAnnotation: Scan method notation
/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//paths, filter out which ones
//yyy
//1. ant() : indicates which packet to filter
//2. any() : indicates that all packets are filtered
//3. none() : indicates that none is filtered
/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//enable: indicates whether to enable the swagger function. The default value is true. If it is changed to false, the swagger function cannot be used normally

Copy the code

The problem

How to use Swagger in production but not in release

  • Determine if it is a production environment
    1. Create multiple configuration files application.properties using springBoot’s built-in environment setting method
    2. Specify which environment is currently active in the master configuration filespring.profiles.active=devDev isapplication-xxx.propertiesThe XXX
    3. Get the current environment in your code and determine if it’s production
    // Create a Docket and pass it an Environment object under the springFrame package
    Profiles profiles=Profiles.of("dev"."test");
    boolean flag=environment.acceptsProfiles(profiles);
    // This will return true if the environment is dev
    Copy the code
  • Inject the enable ()
    return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag);
                .select()
                .apis(RequestHandlerSelectors.xxx())
                .paths(PathSelectors.yyy());
                .build();
    Copy the code

After that, you can change whether Swagger is displayed simply by determining in the configuration file which environment you are currently in

2. Configure groups

When creating a Docket, you can pass in a. Groupname (” groupname “) to set which group the current document belongs to

How do I configure multiple groups? If there are multiple Docket instances there will be multiple groups, so there will be multiple groups in the index in the upper right corner

conclusion

The final config

package com.ymy.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Documentation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import sun.security.acl.AllPermissionsImpl;

import java.util.ArrayList;

/ * * *@Version 1.0
 * @Author:ymy
 * @Date: 2019/8/31 *@Content: * /

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){
        Profiles profiles=Profiles.of("dev"."test");
        boolean flag=environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ymy"))
                .build();
    }
    // Configure Swagger information ==>ApiInfo
    public ApiInfo apiInfo(a){
        // Author information
        Contact contact=new Contact("Yang Mingyu"."http://blog.ymy12138.cn"."[email protected]");
        return new ApiInfo("Api Documentation"./ / title
                "Api Documentation".// Detailed description
                "1.0"./ / version
                "urn:tos".// Developer information, can be a website
                contact,// Author information
                "Apache 2.0"./ / rely on
                "http://www.apache.org/licenses/LICENSE-2.0".newArrayList()); }}Copy the code

In this case, you need to set the current environment to dev’s environment to see Swagger

If not, use the one below


@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(a){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo();
    }
    // Configure Swagger information ==>ApiInfo
    public ApiInfo apiInfo(a){
        // Author information
        Contact contact=new Contact("Yang Mingyu"."http://blog.ymy12138.cn"."[email protected]");
        return new ApiInfo("Api Documentation"./ / title
                "Api Documentation".// Detailed description
                "1.0"./ / version
                "urn:tos".// Developer information, can be a website
                contact,// Author information
                "Apache 2.0"./ / rely on
                "http://www.apache.org/licenses/LICENSE-2.0".newArrayList()); }}Copy the code