swagger-ui.html弹窗报错:Unable to infer base url. This is common when using dynamic..

学习尚硅谷的springboot项目谷粒学院,遇到的swagger报错问题。

首先说一下项目结构,swagger与启动类不在同一个模块中,swagger位于common模块下,启动类位于service模块下的service_edu子模块,guli_parent模块是common模块与service模块的父模块,项目结构如下图。

swagger-ui.html弹窗报错:Unable to infer base url. This is common when using dynamic.._第1张图片

再说一下swagger的整合过程。

1、在common模块的pom.xml文件中引入swagger相关依赖

        
        
            io.springfox
            springfox-swagger2
            provided 
        
        
            io.springfox
            springfox-swagger-ui
            provided 
        

2、写Swagger配置类,类的位置看项目结构图,注意加注解@EnableSwagger2

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://atguigu.com", "[email protected]"))
                .build();
    }
}

3、因为swagger与启动类不在同一个模块中,所以要将swagger引入启动类所在模块的pom中,为了方便引入到了service模块的pom.xml中。swagger所在模块为service_base,将service_base引入service中

        
            com.atguigu
            service_base
            0.0.1-SNAPSHOT
        

4、swagger配置类不在service_edu模块中,启动时无法加载swagger配置类,所以要在启动类上加注解@ComponentScan(basePackages = {"com.atguigu"})改变扫表规则,意思是扫描整个项目下com.atguigu的包,扫描规则不了解的话看一下双亲委派机制

@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu"})
public class EduApplication {
    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class, args);
    }
}

5、此时启动项目在浏览器输入http://localhost:8001/swagger-ui.html,会弹出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...的错误信息

swagger-ui.html弹窗报错:Unable to infer base url. This is common when using dynamic.._第2张图片

需要做的是关闭项目然后将maven刷新一下再启动就可以了=。=就这么简单

swagger-ui.html弹窗报错:Unable to infer base url. This is common when using dynamic.._第3张图片


 

你可能感兴趣的:(Springboot,swagger2,spring,boot,maven)