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/");
}
游手好闲地学习,并不比学习游手好闲好。 —— 约翰·贝勒斯
2 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号