Spring Boot 集成 Swagger 访问时弹出“Unable to infer base url...”错误

本文将介绍怎样解决 Spring Boot 集成 Swagger 访问时弹出“Unable to infer base url...”错误。

Spring Boot 集成 Swagger,启动后使用浏览器访问弹出提示框:

Spring Boot 集成 Swagger 访问时弹出“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:

Maven 依赖如下:

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.7.0</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.7.0</version>
</dependency>

解决办法

  • 查看是否在启动类中定义 @EnableSwagger2 注解

  • 是否映射了 Swagger 的 webjars 资源。如果你通过继承 WebMvcConfigurationSupport 类实现资源映射,一定要注意一个 Spring Boot 应用中只允许出现一个 WebMvcConfigurationSupport 类(即使你配置了多个 WebMvcConfigurationSupport 类也只有一个会被执行),配置如下:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 映射webjars 资源
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}
学习必须与实干相结合。 —— 泰戈尔
1 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号