springboot整合Swagger2弹窗提示Unable to infer base url. This is common when using dynamic servlet registra

本文详细解析了在Spring Boot项目中遇到的Swagger2无法加载的问题,并提供了有效的解决方案。通过添加@ComponentScan注解确保Spring能够扫描到Swagger2配置类,从而成功解决了Swagger资源无法正常加载的困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息:

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弹窗
在这里插入图片描述
根据错误信息在网上找了说是加上@EnableSwagger2注解的但是这个注解我是有的

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.admin01"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多Spring Boot相关文章请关注:http://www.zing-tech.com/")
                .termsOfServiceUrl("http://www.zing-tech.com")
                .contact("ygl")
                .version("1.0")
                .build();
    }
}

随后发现是spring没有扫描到Swagger2配置类,在启动类上加上@ComponentScan(“com.example.config”)注解启动再次访问就可以了

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值