SpringBoot集成Swagger2报错Unable to infer base url. This is common…API Gateway

Java技术 潘老师 2年前 (2021-08-02) 1513 ℃ (0) 扫码查看

在使用IDEA+SpringBoot集成Swagger2时发现SpringBoot启动正常,没有报错,但当使用浏览器防止http://localhost:8080/swagger-ui.html地址时浏览器提示如下:
SpringBoot集成Swagger2报错Unable to infer base url. This is common...API Gateway

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:http://localhost:8080/swagger-ui.html

然后发现IDEA控制台也会不停的报错如下:
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonIncludeProperties
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_202]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_202]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_202]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_202]
… 61 common frames omitted

从报错情况可以看出是因为少jackson的相关jar包导致的(而缺少的原因也可能是SpringBoot版本和Swagger版本不匹配原因导致的,不过在此我们暂不考虑)。
解决办法:

pom.xml导入jackson相关的依赖,具体如下:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.11.2</version>
</dependency>

这里我使用2.11.2版本是因为SpringBoot好像也内置了相关的jar,低版本好像会有冲突。导入成功后我们重启SpringBoot再次访问,发现成功了:
SpringBoot集成Swagger2报错Unable to infer base url. This is common...API Gateway


版权声明:本站所有文章,如无特殊说明,均为本站原创。转载请务必注明文章来源,谢谢支持。
本文链接:https://www.panziye.com/java/3397.html
喜欢 (1)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】