未提供API定义,Swagger无法显示文档。

6

我无法在 Swagger-UI 中显示我的 Spring 文档。

以下是我的配置:

springdoc:
  # api-docs:
    # enabled: true
    # path: /v3/api-docs/swagger-config
  swagger-ui:
    # path: /swagger-ui.html
    disable-swagger-default-url: true
    # config-url: /v3/api-docs/swagger-config
    # url: /v3/api-docs

我输入以下URL以访问界面:http://localhost:8080/swagger-ui.html

swagger ui empty

问题在于它没有显示从以下地址加载的api-docs:

"http://localhost:8080/v3/api-docs/swagger-config"

状态码:200。

但如果我在URL字段中输入此内容,则可以正常工作。我的目标不是每次都要输入这个。

swagger ui working

我尝试了所有可能的方法,但我不明白为什么它不起作用。

编辑:swagger-config

{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}

network tab


请问您能否分享您正在访问的完整URL、网络选项卡中的http控制台日志以及您为swagger添加的依赖项。此外,这篇文章可能会对您有所帮助:https://dev59.com/acTra4cB1Zd3GeqPzCla#YP8xoYgBc1ULPQZFZl42 - GJohannes
swagger-config: http://localhost:8080/v3/api-docs/swagger-config 文档配置: http://localhost:8080/v3/api-docs - Kévin
浏览器网络选项卡中的HTTP响应图片也会很有帮助,可以看到实际请求了什么。 - GJohannes
@GJohannes 我已经添加了图片。 - Kévin
嗨@Kévin,你能解决这个问题吗?还是我们需要采用在搜索栏中传递路径的唯一方法。似乎没有解决方案?在我的情况下,添加springdoc.swagger-ui.url和springdoc.api-docs.path也不起作用。 - Biswarup Nath
4个回答

6
我曾遇到一个类似的问题,并通过更新'springdoc-openapi-spring-boot-2-webmvc'演示项目中的配置来复现它。 我定义了以下属性:
springdoc.api-docs.path=/test/v3/api-docs
sprindoc.swagger-ui.config-url=/test/v3/api-docs/swagger-config
springdoc.swagger-ui.path=/test/swagger-ui.html

使用该配置可以复现OP报告的问题。对于我而言,解决方法是添加一个额外的属性:

springdoc.swagger-ui.url=/test/v3/api-docs

希望这有所帮助。

不,它不起作用,你可以看到我的 OP 中所评论的就是你所说的内容。同样的行为,我需要在搜索栏中输入 /v3/api-docs,然后它才能工作.. :( - Kévin

0
我曾经做过那个。
springdoc:
  swagger-ui:
    # swagger-ui地址
    path: /springdoc/swagger-ui.html
    enabled: true
    # 配置本地访问页面(注释)
    #config-url: /springdoc/api-docs/swagger-config
    # 取消默认Swagger访问页面
    disable-swagger-default-url: true
    # 修复Failed to load remote configuration.
    url: /springdoc/api-docs
  api-docs:
    path: /springdoc/api-docs

否则,我在ResponseBodyAdvice中做其他事情。
@RestControllerAdvice
@Slf4j
public class UnitedResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body != null && !(body instanceof ResponseVo) && !(body instanceof byte[])) {
            // 放行Swagger相关
            if (body instanceof TreeMap && ((TreeMap)body).containsKey("oauth2RedirectUrl")) {
                return body;
            }
            return ResponseVo.message(ResponseCode.SUCCESS.val(), ResponseCode.SUCCESS.des(), body);
        }
        return body;
    }
}

0

在从Springfox迁移到Springdoc时遇到了类似的问题,不同之处在于本地测试一切正常,但是远程出现了相同的问题。

你指向了/v3/api-docs/v3/api-docs/swagger-config有所帮助。
我查看了swagger-config的响应,并发现它是我的应用程序主页,而不是包含配置的JSON对象。
然后我看到有一个安全规则,其中包括swagger的特定url的例外情况。虽然有一些我不再需要的Springfox的url,但没有swagger-config。更新后问题得到解决。

希望这能帮助到某些人。


-2
只需在application.properties中添加springdoc.swagger-ui.path = /myPath,然后导航到localhost:8080/mypath,它应该会自动重定向您。

没有解决指出的问题的方法。问题不在于重定向失败,而是没有自动加载API配置以正确显示API定义。 - malik_cesur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接