再现springboot集成swagger

  1. 导入swagger的依赖包
 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
 </dependency>

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.9.2</version>
 </dependency>
  1. 添加配置
package com.starcpdk.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.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
    
    

    // 配置多分组就需要提供多个提供Docker的bean   docker1  docker2  docker3
    @Bean
    public Docket docket1(Environment environment){
    
    
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(Environment environment){
    
    
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3(Environment environment){
    
    
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    // 配置了swagger的docker的bean实例
    @Bean
    public Docket docket(Environment environment){
    
    

        // 获取项目的环境
        // 设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev" , "test");
        // 通过environment.acceptsProfiles(profiles)判断是否处在自己设定的环境中
        // 表示 , 如果我当前的环境是dev或者是test , 则返回一个true
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 配置组名
                .groupName("姚云峰")
                // enable()是否启动swagger , 如果为false , 则swagger不能在浏览器中访问
                .enable(flag)
                .select()
                // RequestHandlerSelectors 配置要扫描接口的方式
                // basePackage 指定要扫描的包
                // any()扫描全部
                // none()不扫描
                // withClassAnnotation() 扫描类上的注解 , 参数是注解的反射对象
                // withMethodAnnotation()扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.starcpdk.swagger.controller"))
                // path()过滤什么路径 , 只扫描starcpdk下面的请求 , 就是controller下面的路径带有这个的请求 例如这样的 @GetMapping("/starcpdk/hello")
                .paths(PathSelectors.ant("/starcpdk/**"))
                .build();
    }

    // 配置swagger信息  =  apiInfo
    public ApiInfo apiInfo(){
    
    
        // 作者信息
        Contact contact = new Contact("姚云峰", "http://www.apache.org/licenses/LICENSE-2.0", "[email protected]");
        return new ApiInfo(
                "姚云峰的swaggerApi文档",
                "星星之火可以燎原",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

  1. 添加注解
@Api(description = "xxx")      //添加到类之前 ,对该controller的 功能进行描述
 
@ApiOperation(value = "xxx")   //添加到方法前面 , 对此接口的功能描述

@ApiParam("xxx")      // 添加到参数前面 , 对参数进行描述

@ApiModel(value = "xxx") // 放在实体类的上面

@ApiModelProperty(value = "xxxx") // 添加到实体类中的属性前对该属性进行描述

@ApiModelProperty(value = "xxx", example = "xxx")   // 描述参数示例

猜你喜欢

转载自blog.csdn.net/weixin_44735933/article/details/110307068