【问题标题】:Specify the return content type of a Controller指定Controller的返回内容类型
【发布时间】:2019-02-19 20:25:30
【问题描述】:

我有以下metrics.controller.ts 文件:

import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiUseTags, ApiModelProperty } from '@nestjs/swagger';
import { PrometheusService } from './prometheus.service';

@Controller('metrics')
@ApiUseTags('Misc')
export class MetricsController {
  constructor(private readonly prometheusService: PrometheusService) {}

  @Get('/')
  @ApiOperation({
    title: 'Prometheus metrics',
    description: 'Exposes default prometheus node metrics'
  })
  @ApiResponse({ status: 200, description: 'Prometheus metrics' })
  public getMetrics(): string {
    return this.prometheusService.getMetrics();
  }
}

然而,当实际输出为 text/plain 时,Swagger 错误地指示 响应内容类型application/json

我尝试查看Swagger docs regarding describing responses,我正在寻找的项目是“内容”部分。但是,该文档不涵盖 TypeScript 注释。 Swagger Core 2.X documentation 谈到了一个 @Operation 注释,但我没有提到它,我不知道从哪里得到一个 - 只有 @ApiOperation 有效。

项目目录下npm ls的相关版本信息如下:

+-- @nestjs/swagger@2.5.1
| +-- @nestjs/common@5.3.6 deduped
| +-- @nestjs/core@5.3.6 deduped
| +-- lodash@4.17.10
| +-- path-to-regexp@2.2.1 deduped
| `-- swagger-ui-express@3.0.10

指示响应内容类型text/plain的正确注释是什么?

【问题讨论】:

    标签: node.js typescript swagger


    【解决方案1】:

    我发现解决方案是import ApiProduces from '@nestjs/swagger',如下所示:

    import { ApiOperation, ApiResponse, ApiUseTags, ApiProduces } from '@nestjs/swagger'
    
    //...
    
        @ApiProduces('text/plain')
        public getMetrics(): string {
            //...
        }
    

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多