NestJS设置Swagger

2021-10-09  本文已影响0人  Poppy11

1、下载依赖

npm install --save @nestjs/swagger swagger-ui-express

2、在main.ts中初始化Swagger

 const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

3、应用程序运行时,打开浏览器并导航到 http://localhost:3000/api 。 你应该可以看到 Swagger UI
4、编写DTO时我们就需要使用装饰器对字段进行声明,更多装饰器用法可参考
https://docs.nestjs.cn/8/recipes?id=swagger
例如

export class jiraJsonConfigDto{
  @ApiProperty({
    description: 'Positions.json/Processes.json/Welcome.json',
  })
  @IsNotEmpty()
  readonly jiraType : string;
}
上一篇 下一篇

猜你喜欢

热点阅读