【发布时间】: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