用于邮递员的 NestJs Swagger

NestJs Swagger for Postman

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

我尝试导入它,但 Postman 中没有显示端点?

我正在使用 NestJs 和 Swagger + Postman

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

To generate and download a Swagger JSON file, navigate to http://localhost:3000/api-json (swagger-ui-express) or http://localhost:3000/api/json (fastify-swagger) in your browser (assuming that your Swagger documentation is available under http://localhost:3000/api).

我们提供了有关使用 NestJS 的 openApi 的更多信息here

在 main.ts 中设置 swagger 时(请参阅此处的文档:NestJs Swagger Docs),您可以添加 .setExternalDoc('Postman Collection', '/your-api-docs-url-with-the-word-json-at-the-end')。这会在文件顶部为您提供一个 link,因此您可以单击以获取可导入的 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 的信息,请参阅此 Whosebug post: