nestjs + swagger ui
点击 try it out 和 execute 后,
好像只有 status 200 的时候,能看到正常的返回值,
而 201 或者其它状态码就看不到返回值是怎么回事?
其它状态码就显示的 error: 加一个状态码
// controller
@Get('test1')
test1() {
return '111';
}
@Post('test2')
test2() {
return '222';
}

1
lovedebug 1 月 6 日
201 本身就可以无 response body 的~
|
2
bronana OP @lovedebug https://petstore.swagger.io/#/pet/addPet 可是这个可以看到能显示的啊
|
3
pingdog 1 月 6 日 via iPhone
nestjs 响应 post 缺省 201 ,没回显,要回显就手设 200
|
4
pingdog 1 月 6 日 via iPhone
你注意看 swagger example 的 code 200 ,http code 201 一般都不接收 body 了
|
6
jkhuangfu 1 月 7 日
|
7
willsank0430 8 天前
import { CallHandler, ExecutionContext, HttpStatus, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs'; import { FastifyRequest, FastifyReply } from 'fastify'; /** * 针对 post 请求默认响应 201 的问题处理 * */ @Injectable() export class PostInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { const request = context.switchToHttp().getRequest<FastifyRequest['raw']>(); const response = context.switchToHttp().getResponse<FastifyReply['raw']>(); if (request.method === 'POST') { if (response.statusCode === 201) { context.switchToHttp().getResponse().status(HttpStatus.OK); } } return next.handle(); } } |
8
willsank0430 8 天前
@willsank0430 post.interceptor.ts
|
9
humbass 2 天前
一直感觉使用 nestjs 撸代码,js 的轻松自由全没了, 还不如直接上 SpingBoot!
|