<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>版本号version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>版本号version>
dependency>
但换成 2.10.x 版本后报错:
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway…
访问路径还是:http://localhost:8080/swagger-ui.html
。但注意: 这个版本有点特殊:没有 @EnableSwagger2
注解,所以配置类中需要出掉这个注解,然后添加 @EnableSwagger2WebMvc
注解。
并增加下面这个依赖:
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-spring-webmvcartifactId>
<version>2.10.5version>
dependency>
提示: 从 Swagger2.10.0 至 2.10.5,配置时,配置类(配置类在文章结尾给出)使用的注解开始分为 @EnableSwagger2WebMvc
和@EnableSwagger2WebFlux
,用于区分 阻塞式编程框架 和 响应式编程框架,并直接去除了@EnableSwagger2 注解。但是此时出现一个问题:不利于维护,因此 2.10.x 版本可以理解为一个测试版本。
需要传入后台的为 string 类型,但是使用 swagger-ui 接口进行测试的时候,输入的为数字类型,对 pom.xml 文件进行调整:
将原来默认的 1.5.20 版本剔除,此时的 swagger.version 默认为 2.10.5,默认引入的为1.5.20,可以剔除再引入新的1.5.21
// @EnableSwagger2
@EnableSwagger2WebMvc //使配置生效
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket getDocket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo()) //设置Swagger接口文档的首页信息
.select() //初始化并返回一个API选择构造器
.apis(RequestHandlerSelectors.any()) //选择将哪些接口生成API文档
.paths(PathSelectors.any()) //可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build(); //创建API文档
return docket;
}
//由于上面.apiInfo需要这个参数,所以这里写这个方法
private ApiInfo getApiInfo(){
//创建作者对象
Contact contact = new Contact("zfp","介绍这个人的链接","[email protected]");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("《后端接口文档》") //文档的标题
.description("文档的描述信息") //文档的描述
.contact(contact) //文档的作者
.version("V1.0") //文档的版本
.build(); //构建
return apiInfo;
}
}