我想要的
我正在使用NestJS实现一个删除路由。
由于该路由仅在成功删除时才返回HTTP 204 No Content
,因此我使用@nestjs/swagger
装饰器对其进行了如下注释:
@ApiParam({
name: 'id',
required: true,
type: 'string',
example: 'abc123',
})
@ApiNoContentResponse({
description: 'All items for the given id have been deleted',
})
@Delete('/accounts/:id/items')
async deleteItems(
@Param('id') id: string,
) {
// do stuff
}
堆栈
@nestjs/cli: 9.1.6
@nestjs/common: 9.1.6
@nestjs/core: 9.1.6
@nestjs/platform-express: 9.1.6
@nestjs/swagger: 6.1.2
nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": [
"@nestjs/swagger/plugin"
]
}
}
问题
如何防止这种情况(默认情况)?被产生的反应?
会发生什么?
在使用npm run start:dev
运行服务器时,将创建对我的路由的傲慢描述。尽管我没有显式地定义它,但该描述现在在路由的HTTP 200
部分中包含了一个Response
条目。该条目不包含说明,也不包含未显式定义的指示符。
我所期望的
在使用npm run start:dev
运行服务器时,将创建对我的路由的傲慢描述。该说明仅包含HTTP 204
在路由的Response
部分中的内容和条目。
我试过的
显式定义@ApiOkResponse
// they all have no effect
@ApiOkResponse()
// OR
@ApiOkResponse({})
// OR
@ApiOkResponse({ status: 204 })
定义一个@ApiDefaultResponse
// they all create a response 'default' with no description
@ApiDefaultResponse()
// OR
@ApiDefaultResponse({})
// OR
@ApiDefaultResponse({ status: 204 })
发布于 2022-11-10 09:08:46
您只需将@HttpCode(204)
作为注释添加到代码中即可。默认情况下添加200,因为这是DELETE
requests.This默认行为的默认响应状态代码,将用上面提到的注释覆盖。
有关状态代码的更多信息可以在这里找到:https://docs.nestjs.com/controllers
https://stackoverflow.com/questions/74386377
复制相似问题