你可以从别人那里汲取某些思想,但必须用你自己的方式加以思考|

makalo

园龄:5年11个月粉丝:144关注:15

NestJs swagger接口文档

文档:https://docs.nestjs.cn/9/recipes?id=swagger

安装

首先,您必须安装所需的包:

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

如果你正在使用 fastify ,你必须安装 fastify-swagger 而不是 swagger-ui-express

npm install --save @nestjs/swagger fastify-swagger

在main.ts 注册swagger

import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';


// 注册Swagger
const options = new DocumentBuilder().setTitle('makalo接口文档').setDescription('描述,。。。').setVersion('1').build();
const document = SwaggerModule.createDocument(app,options);
SwaggerModule.setup('/api-docs',app,document);

image-20230407150624987

访问

http://localhost:3000/api-docs

image-20230407150827103

现在没有分组比较乱

使用ApiTags 添加分组

@ApiTags('makalo 分组')

image-20230407152905448

image-20230407152929728

ApiOperation 接口描述

 @ApiOperation({summary:"创建makalo的接口",description:"这是一个创建的接口的描述"})

image-20230407153256076

image-20230407153327216

ApiParam 动态参数描述

@ApiParam({name:"id",description:"用户id",required:true})

image-20230407153511612

ApiQuery 修饰get

@ApiQuery({name:"testGet",description:"bbb"})

image-20230407153713942

image-20230407153746137

ApiProperty 修饰DTO

import { ApiProperty } from '@nestjs/swagger';
export class CreateMakaloDto {
    @ApiProperty({ description: "姓名", example: "makalo" })
    name: string
    @ApiProperty({ description:"年龄", example: "18"})
    age: number
}

image-20230407161536559

image-20230407161646628

ApiResponse 自定义返回信息

@ApiResponse({status:403,description:"自定义返回信息"})

image-20230407161939125

ApiBearerAuth jwt token

main.ts 注册

.addBearerAuth()
例:
const options = new DocumentBuilder().addBearerAuth().setTitle('makalo接口文档').setDescription('描述,。。。').setVersion('1').build();

image-20230407162821993

控制器中修饰

@ApiBearerAuth()

image-20230407163134199

添加token

image-20230407163613750

image-20230407163316358

image-20230407163339000

测试

image-20230407163734832

image-20230407163756450

image-20230407163828487

其他装饰器

所有可用的 OpenAPI 装饰器都有一个 Api 前缀,可以清楚地区分核心装饰器。 以下是导出的装饰器的完整列表,以及可以应用装饰器的级别的名称。

@ApiOperation() Method
@ApiResponse() Method / Controller
@ApiProduces() Method / Controller
@ApiConsumes() Method / Controller
@ApiBearerAuth() Method / Controller
@ApiOAuth2() Method / Controller
@ApiBasicAuth() Method / Controller
@ApiSecurity() Method / Controller
@ApiExtraModels() Method / Controller
@ApiBody() Method
@ApiParam() Method
@ApiQuery() Method
@ApiHeader() Method / Controller
@ApiExcludeEndpoint() Method
@ApiTags() Method / Controller
@ApiProperty() Model
@ApiPropertyOptional() Model
@ApiHideProperty() Model
@ApiExtension() Model

本文作者:makalo

本文链接:https://www.cnblogs.com/makalochen/p/17296693.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   makalo  阅读(650)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
历史上的今天:
2020-04-07 error: failed to push some refs to 'git@gitee.com:xxx/xxx.git' 问题 解决办法
2020-04-07 git 常用命令
2020-04-07 git win10和ubuntu 16.04的安装和配置连接gitee
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起