【问题标题】:Intercept messages in Swagger UI在 Swagger UI 中拦截消息
【发布时间】:2018-12-11 09:11:35
【问题描述】:

我正在尝试使用此代码在 Swagger 中拦截响应消息:

var full = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');

var ui = SwaggerUIBundle({

    url: full + "/swagger/v2/swagger.json",

    responseInterceptor: function (resp) {
        console.log('#response');
        return resp;
    },

    onComplete: function () {
        console.log('#onComplete');

    }
});

问题是响应拦截器只被调用一次(对于https://localhost:5001/swagger/v2/swagger.json 文件)并且没有为任何API 消息调用它。

是否可以拦截所有swagger API消息?

根据这篇文章应该是可能的:https://stackoverflow.com/a/46892528/1882699,但由于某种原因这对我不起作用。

【问题讨论】:

  • responseInterceptor 在 Petstore 演示的最新版本 (3.20.2) 中对我来说很好用,例如如果我克隆repo 并将responseInterceptor 添加到dist/index.html,然后使用该index.html。您使用哪个版本的 Swagger UI? (打开浏览器控制台并计算versions 变量。)浏览器控制台中是否还有其他错误? Swagger UI 是否真的从您的 API 接收响应?可能是来自 UI 的请求被 CORS 阻止了;这将通过 UI 中的“无法获取”消息来指示。

标签: swagger swagger-ui


【解决方案1】:

适合我的 Swagger UI 配置在 this 帖子中。

不同的是这一行:

dom_id: '#swagger-ui',

使用此行时,拦截器会拦截每条消息。如果没有这一行,拦截器只会捕获第一条消息。

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多