1. Spring Boot 集成 Swagger2 常见依赖为:


<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.10.5</version>  
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.10.5</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…

浏览器访问报错图片:
关于Swagger2.10.x版本,spring boot集成Swagge2.10.x(比如2.10.5)依赖问题,没有 @EnableSwagger 新注解@EnableSwagger2WebMvc插图
解决:

访问路径还是:http://localhost:8080/swagger-ui.html但注意: 这个 2.10.x 版本有点特殊:没有 @EnableSwagger2 注解,所以配置类中需要出掉这个注解,然后添加 @EnableSwagger2WebMvc 注解。

并然后增加下面这个依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-spring-webmvc</artifactId>
    <version>2.10.5</version>  
</dependency>

提示: 从 Swagger2.10.0 至 2.10.5,配置时,配置类(配置类在文章结尾给出)使用的注解开始分为 @EnableSwagger2WebMvc@EnableSwagger2WebFlux,用于区分 阻塞式编程框架响应式编程框架,并直接去除了@EnableSwagger2 注解。但是此时出现一个问题:不利于维护,因此 2.10.x 版本可以理解为一个测试版本。(常用版本为 2.9.4 或者最新的 3.0.0)

2. Spring Boot 使用 Swagger2.10.5 有关版本更新带来的其他问题:

现在需要传入后台的数据为 string 类型,但是使用 swagger-ui 接口进行测试的时候,输入的数据的类型其实是数字类型,不匹配,所以需要修改:对 pom.xml 文件进行调整:
关于Swagger2.10.x版本,spring boot集成Swagge2.10.x(比如2.10.5)依赖问题,没有 @EnableSwagger 新注解@EnableSwagger2WebMvc插图(1)
将原来默认的 1.5.20 版本剔除,此时的 swagger.version 默认为 2.10.5,默认引入的为1.5.20,可以剔除再引入新的1.5.21

3. Spring Boot 集成 Swagger2 时的配置类:

// @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","介绍这个人的链接","30888@qq.com");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("《后端接口文档》")  //文档的标题
                .description("文档的描述信息")  //文档的描述
                .contact(contact)  //文档的作者
                .version("V1.0") //文档的版本
                .build();   //构建
        return apiInfo;
    }
}
本站无任何商业行为
个人在线分享 » 关于Swagger2.10.x版本,spring boot集成Swagge2.10.x(比如2.10.5)依赖问题,没有 @EnableSwagger 新注解@EnableSwagger2WebMvc
E-->