Nestjs 接口文档

添加 swagger

1
npm install --save @nestjs/swagger

main.ts添加swagger

1
2
3
4
5
6
7
const config = new DocumentBuilder()
.setTitle('Blog')
.setDescription('The Blog API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);

在服务中导航到/api-docs查看接口文档

接口文档添加分类(ApiTags)和描述(ApiOperation)

给controller添加ApiTags装饰器

1
2
3
4
5
6
7
8
9
@Controller('blogs')
@ApiTags('blogs')
export class BlogController {
@Get()
@ApiOperation({ summary: '获取用户列表'})
findAll(){
return ''
}
}

接口文档添加requestBody

1
2
3
4
5
6
7
8
9
10
11
12
13
export class createUserDto{
@ApiProperty()
name: string

@ApiProperty()
age: number
}

@Post()
@ApiOperation({ summary: '创建用户'})
findAll(@Body() body: createUserDto){
return body
}

本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2023/10/Nodejs/NestJs/swagger/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!