SpringBoot使用Swagger2出现Unable to infer base url.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot使用Swagger2出现Unable to infer base url.相关的知识,希望对你有一定的参考价值。

参考技术A 在使用SpringBoot中配置Swagger2的时候,出现

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:

引用来自 https://github.com/springfox/springfox/issues/1996#issuecomment-335155187

4.但是在我的项目中,发现是我使用Spring的ResponseBodyAdvice全局返回再处理的一个类,本意是为所有返回JSON数据统一添加“状态=succuess”等信息,没想到该实现影响了Swagger的使用,会导致swagger返回的JSON数据格式和期望的不一致,故swagger报错。解决方法在该接口实现类上面的@ControllerAdvice 注解限制接口的扫描包即可避免。

5.在排查该问题时,假设你的swagger-ui访问路径是 http://localhost:8080/swagger-ui.html, 可以先直接访问 http://localhost:8080/v2/api-docs, 查看swagger是否正确获取到了JSON格式的数据,且JSON格式数据是否为类似以下格式,如果不对则可能是因为别的返回预处理接口对数据进行了处理,导致swagger无法获取到正确的数据。

以上是关于SpringBoot使用Swagger2出现Unable to infer base url.的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot之Swagger2的使用

13.9 SpringBoot集成Swagger2中遇到的问题

企业级springboot落地:swagger2整合并不是那么随便

SpringBoot-整合Swagger2

SpringBoot使用Swagger2构建API文档

SpringBoot使用Swagger2实现Restful API