The basic configuration

  • Import dependence
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>
Copy the code

Visit Swagger UI,http://localhost:8080/swagger-ui/index.html

  • Write configuration classes and configure basic information
@Configuration
@EnableOpenApi
public class SwaggerConfig {
     @Bean
     public Docket docket(a){
       return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
     }
     private ApiInfo apiInfo(a){
         Contact contact= new Contact("kittyguy"."https://www.kittyguy.com"."[email protected]");
         return  new ApiInfoBuilder()// chain programming
                 .title("Very strong API.")
                 .description("This API could make a billion dollars.")
                 .termsOfServiceUrl("https://www.baidu.com/")
                 .license("License 2.1 of Ministry of Industry and Information Technology of China")
                 .contact(contact)
                 .version("3.0") .build(); }}Copy the code

Configure the interface to be scanned

@Configuration
@EnableOpenApi
public class SwaggerConfig {
     @Bean
     public Docket docket(a){
       return new Docket(DocumentationType.SWAGGER_2)
               .apiInfo(apiInfo())
               .select()

         /* select() starts the builder for API selection. Return value: API selection generator. To complete the build of the API selector, call the BUILD method of the API selector, which automatically falls back to the build summary when called. * /
               // RequestHandlerselectors. Configure the mode of interface scanning
               // basePackage: specifies the package to scan
               // any(): scans all
               // None (): does not scan
               //withclassAnnotation: Scans the annotation on the class. The parameter is a reflection object of the annotation
               // withMethodAnnotation: the annotation on the scan method
               .apis(RequestHandlerSelectors.basePackage("com.boot.controller"))// The interface for scanning is based on the packet
               .paths(PathSelectors.ant("/hello"))// Specify which URL requests will be scanned.build(); }}Copy the code

Use Swagger in development environments, not in use environments

  • Three configuration files

application.properties

spring.profiles.active=dev
Active configuration file
Copy the code

application-dev.properties

server.port=8082
Copy the code

application-prod.properties

server.port=8081
Copy the code
  • The configuration class
@Configuration
@EnableOpenApi
public class SwaggerConfig {
     @Bean
     public Docket docket(Environment environment){
         Profiles profiles = Profiles.of("prod"."test");
         boolean isOnActive = environment.acceptsProfiles(profiles);// Determine the owning environment, that is, whether the corresponding file is active

       return new Docket(DocumentationType.SWAGGER_2)
               .apiInfo(apiInfo())
               .enable(isOnActive)// Whether to enable swagger
               .select()

         /* select() starts the builder for API selection. Return value: API selection generator. To complete the build of the API selector, call the BUILD method of the API selector, which automatically falls back to the build summary when called. * /
               // RequestHandlerselectors. Configure the mode of interface scanning
               // basePackage: specifies the package to scan
               // any(): scans all
               // None (): does not scan
               //withclassAnnotation: Scans the annotation on the class. The parameter is a reflection object of the annotation
               // withMethodAnnotation: the annotation on the scan method
               .apis(RequestHandlerSelectors.basePackage("com.boot.controller"))// The interface for scanning is based on the packet
               .paths(PathSelectors.ant("/hello"))// Specify which URL requests will be scanned.build(); }}Copy the code