关于SpringBoot整合Swagger2出现的问题综合及解决方案

一、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: 

小弟曾遇到过两次这种情况。

第一次的原因是没有加入swagger-ui的配置文件

//@Configuration
//public class SwaggerConfiguration extends WebMvcConfigurerAdapter implements EnvironmentAware {
//  private String basePackage;
//  private String creatName;
//  private String serviceName;
//  private RelaxedPropertyResolver propertyResolver;
//  private String description;
//  /**
//   * 这个地方要重新注入一下资源文件,不然不会注入资源的,也没有注入requestHandlerMappping,相当于xml配置的
//   *  <!--swagger资源配置-->
//   *  <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
//   *  <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
//   *  不知道为什么,这也是spring boot的一个缺点(菜鸟觉得的)
//   * @param registry
//   */
//  @Override
//  public void addResourceHandlers(ResourceHandlerRegistry registry) {
//    registry.addResourceHandler("swagger-ui.html")
//      .addResourceLocations("classpath:/META-INF/resources/");
//    registry.addResourceHandler("/webjars*")
//      .addResourceLocations("classpath:/META-INF/resources/webjars/");
//  }
//
//
//
//  @Bean
//  public Docket createRestApi() {
//    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
//      .select()
//      .apis(RequestHandlerSelectors.basePackage(this.basePackage))
//      .paths(PathSelectors.any()).build();
//  }
//
//  private ApiInfo apiInfo() {
//    return new ApiInfoBuilder()
//      .title(this.serviceName+" Restful APIs")
//      .description(this.description)
//      .contact(this.creatName).version("1.0").build();
//  }
//
//  @Override
//  public void setEnvironment(Environment environment) {
//    this.propertyResolver = new RelaxedPropertyResolver(environment, null);
//    this.basePackage = propertyResolver.getProperty("swagger.basepackage");
//    this.creatName = propertyResolver.getProperty("swagger.service.developer");
//    this.serviceName = propertyResolver.getProperty("swagger.service.name");
//    this.description = propertyResolver.getProperty("swagger.service.description");
//  }
//}

加上上述配置文件就OK。

小弟刚刚遇到的情况是这样的,我在项目中加了一个拦截器,拦截了地址,选择性的放行了包含swagger的uri;便出现了上述情况。

先解释一下原因。当访问swagger-ui.html时,html页面会发送四个请求:

1./swagger-resources/configuration/ui   获取页面UI配置信息

2./swagger-resources     

3./v2/api-docs        获取接口配置信息

4./swagger-resources/configuration/security     安全配置


所以在设置放行uri的时候需要同时放行/v2/api-docs这个uri,否则就会报错。

二、org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException


这个原因是因为swagger配置文件中缺少了必要信息。

例如,本例中是因为少写了controller所在的包。

三、swagger-ui.html中不出现任何信息。

如下图所示一样,



这主要是因为没有加上@EnableSwagger2这个注解。

三、接口详细参数测试页面无法显示

如下图所示:


点击GET方法的test接口,怎么也无法显示其中的接口参数信息。这个问题我曾经一度丈二和尚,摸不着头脑。

在认真观察错误后,终于发现了其中的猫腻。


主要是因为编码的问题,所以尽量不要在@Api中加上中文信息。@ApiOperation和@ApiParam这俩注解则问题不大。

改成英文之后便可以显示了。


  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要实现springboot整合swagger2 3.0.0版本,你需要按照以下步骤操作: 1. 创建一个maven项目并引入spring-boot-starter-web和springfox-boot-starter依赖。在pom.xml文件中添加以下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version>2.5.6</version> --> <!-- <version>2.6.3</version> --> <!-- <version>2.6.5</version> --> <version>2.7.3</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> ``` 2. 在application.yml配置文件中添加以下内容: ```yaml spring: mvc: pathmatch: matching-strategy: ant_path_matcher ``` 3. 创建启动类,并在其中添加`@EnableSwagger2`注解。例如: ```java @SpringBootApplication @EnableSwagger2 public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 这样就完成了springboot整合swagger2 3.0.0版本的配置。你可以根据需要在项目中编写相应的接口文档注解以及其他相关配置。如果需要更详细的操作步骤和示例代码,你可以参考中提供的链接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Springboot整合Swagger2(3.0.0版本)](https://blog.csdn.net/mo_sss/article/details/130820204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Springboot整合Swagger UI 3.0.0 版本](https://blog.csdn.net/qq_42102911/article/details/126410050)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值