如何在 SwaggerUIBundle 中设置请求拦截器? [英] How to set request interceptor in SwaggerUIBundle?

查看:46
本文介绍了如何在 SwaggerUIBundle 中设置请求拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的 swagger.json 中,我们将 basePath 设置为 /api,但是,当应用程序部署在 docker 容器中时,上下文路径不是/api.这可能是不同的事情,我们不知道它是什么,所以我们不能对其进行硬编码.

In our swagger.json we are setting basePath to /api, however, when the application is deployed in docker container, the context path is not /api. This could be different thing and we don't know what it is so we can't hard code it.

我正在尝试按照以下指南设置 requestInterceptor,以便捕获请求并修改 url 路径:

I am trying to set requestInterceptor as per the following guide, in order to catch the request and modify the url path perhaps:

https://swagger.io/docs/swagger-tools/#customization-36

但似乎 requestInterceptor 被忽略了.这可能吗?如果没有,如何在运行时设置正确的路径?

But it seems requestInterceptor is being ignored. Is this possible? If not, how can I set the correct path at runtime?

这是我在 index.html

    window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    url: "../api-docs/swagger.json",
    dom_id: '#swagger-ui',
    deepLinking: true,
    requestInterceptor: function(request) {
        window.alert(request);
    },
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui

}

我们使用的是 Swagger 2.0

推荐答案

此处,或更新您的节点包.我遇到了同样的问题,因为我在添加 requestInterceptor 支持之前下载了分发版.

Upgrade to the latest version from here, or update your node package. I had the same problem because I downloaded the distribution before requestInterceptor support was added.

这篇关于如何在 SwaggerUIBundle 中设置请求拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆