Spring Boot 集成 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:
翻译出来就是
无法推断基本url。这在使用动态servlet注册或API位于API网关之后时很常见。基本url是提供所有swagger资源的根。例如,如果api在以下位置可用:http://example.org/api/v2/api-docs那么基本url是http://example.org/api/.请手动输入位置:
解决方法,开启Swagger2
第一、在SpringBootApplication启动类上加上@EnableSwagger2注解

第二、创建一个Swagger的配置类,加上 @Configuration 和 @EnableSwagger2

基本上来说一般访问失败都是没有开启Swagger导致的。
接下来就是其他过滤器拦截器等等导致的失败问题
问题一:
由于对于返回结果做了统一封装,会通过 @RestControllerAdvice 去访问需要的东西,页面解析不出来,因此访问会出错;
重写 ResponseBodyAdvice 的 supports 方法,对Swagger不进行处理

本文介绍了在SpringBoot中集成Swagger2时可能会遇到的访问失败问题及其解决方案。当出现无法推断基本url的错误时,通常是因为Swagger未正确开启。解决方法包括在启动类上添加@EnableSwagger2注解,并创建Swagger配置类。此外,当全局返回结果封装影响Swagger页面显示时,可以重写ResponseBodyAdvice避免处理Swagger。通过这些步骤,可以确保Swagger2的正常运行。

372

被折叠的 条评论
为什么被折叠?



