swagger啟動錯誤Unable to infer base url.

秋田狗子發表於2020-10-27

一、swagger版本

<!--swagger jar包-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

二、錯誤

資訊

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註解

package com.kj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class SwaggerDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerDemoApplication.class, args);
    }

}

其他無關檔案,僅供參考
swagger配置檔案

package com.kj.config;


import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 //開啟swagger
public interface SwaggerConfig {

}

看到這是不是發現了華點。
我這個配置類是interface的。
所以把這個interface改為class也可以

相關文章