NestJs Swagger for Postman

问题描述 投票:0回答:3

有没有办法将生成的swagger.json作为集合导入到Postman中?

我尝试导入它,但端点未在 Postman 中显示?

我正在使用 NestJs 和 Swagger + Postman

swagger postman nestjs nestjs-swagger
3个回答
8
投票

在 main.ts 中设置 swagger 时(请参阅此处的文档:NestJs Swagger Docs),您可以添加

.setExternalDoc('Postman Collection', '/your-api-docs-url-with-the-word-json-at-the-end')
。这会在文件顶部提供一个链接,以便您可以单击以获取可导入的 JSON。

这是我的一个例子:

const document = SwaggerModule.createDocument(
    app,
    new DocumentBuilder()
      .setTitle('Nest Api')
      .setDescription('MyNestApiDescription')
      .setVersion('1.0')
      .addBearerAuth()
      .setExternalDoc('Postman Collection', '/docs-json')
      .build(),
  );
  SwaggerModule.setup('/docs', app, document);

如您所见,我的 API 文档位于

'/docs'
,因此我的 json url 很简单
'/docs-json'

有关如何将其导入 Postman 的信息,请参阅此 stackoverflow 帖子:Postman JSON 文档导入


2
投票

您可以使用

fastify-swagger
在 Postman 中使用它来导出您的 swagger 数据。

要生成并下载 Swagger JSON 文件,请在浏览器中导航到 http://localhost:3000/api-json (swagger-ui-express) 或 http://localhost:3000/api/json (fastify-swagger) (假设您的 Swagger 文档位于 http://localhost:3000/api 下)。

有关 OpenApi 与 NestJS 的更多信息可在此处


0
投票

根据 NestJS 文档 -

@nestjs/swagger
允许您通过在 URL 中添加 -json 来下载 swagger JSON 配置。

例如, 如果您的 swagger 在 http://localhost:3000/api 上可用,您可以从

http://localhost:3000/api-json
:

下载 JSON 配置

然后您可以通过单击主菜单“导入”->“原始文本”和过去的 json 内容将其导入到 Postman 集合中

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.