一、整合
1. 引入依赖
pom.xml
1 | <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> |
2. 配置类
1 |
|
3.访问
访问地址http://localhost:8080/swagger-ui.html
即可
二、常见错误
1.Unable to infer base url.
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
- 是否加了
@EnableSwagger2
注解 - 配置类是否被 SpringBoot 扫描
- 权限拦截排除,例如
.excludePath("/swagger-resources/**", "/v2/api-docs", "/webjars/**", "swagger-ui.html")
- 是否做了全局统一返回封装,需要
@RestControllerAdvice(basePackages = "com.example.controller")
指定拦截包路径,避免拦截到 Swagger