1) 集成步骤
- 添加依赖
 
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.8.0</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.8.0</version>
</dependency>
- 基础配置类
 
package com.imooc.springboot.config;
import io.swagger.annotations.Api;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * @author hahadasheng
 * @since 2020/8/25
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //.host("host") // 设置host, 如果前端文档有代理,在SwaggerFilter中进行过滤并删除这个属性,否则接口无法调用
                .select()
                /*
                下面这行代码是告诉 Swagger 要扫描有 @Api 注解的类,
                可以将 Api.class 替换成 ApiOperation.class 扫描带有 @ApiOperation 注解的方法。
                当然还可以使用 basePackage() 方法配置 Swagger 需要扫描的包路径。
                */
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot")
                .description("RESTFul接口文档说明")
                .version("1.0")
                .build();
    }
}
- 使用接口方法
 
package com.imooc.springboot.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author hahadasheng
 * @since 2020/8/24
 */
@Api
@RestController
public class HelloController {
    @ApiOperation("Hello Spring Boot 方法")
    @GetMapping("/")
    public String hello(@RequestParam(required = false) @ApiParam("名字")
                        String name) {
        if (!StringUtils.isEmpty(name)) {
            return String.format("Hello %s", name);
        }
        return "Hello Spring Boot";
    }
}
- 访问
http://localhost:8080/swagger-ui.htmlhttp://localhost:8080/v2/api-docs
 
2) 常用注解
| 注解 | 作用域 | 说明 | 
|---|---|---|
@Api | 
类 | 标识类为Swagger资源(Controller) | 
@ApiModel | 
类 | 描述接口实体类(通常为参数或返回值) | 
@ApiOperation | 
方法 | 用于描述接口方法 | 
@ApiModelProperty | 
属性/方法 | 描述接口实体属性 | 
@ApiParam | 
参数 | 描述接口参数 | 
3) 避坑
- 目中有使用
@RestControllerAdvice注解进行全局异常处理,导致 Swagger 失效 
在全局异常处理类的 @RestControllerAdvice 注解中指定 Controller 的包名即可:
@RestControllerAdvice(basePackages = "com.imooc.springboot.controller")