代码之家  ›  专栏  ›  技术社区  ›  suleydaman

Swagger-为什么Swagger在我还没有为请求体字段编写注释的情况下创建请求体字段?

  •  0
  • suleydaman  · 技术社区  · 7 年前

    我已经为不需要请求主体的GET请求编写了一个夸张的ui。我没用过 @RequestBody 所以为什么Swagger会在ui上显示请求体字段呢?即使我将其保留为空,也会导致我的API请求失败,并出现以下错误: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

    我理解为什么这个错误存在, curl 那个狂妄自大的人 -d 选择。但我怎么才能关掉它呢?

    我唯一使用的注释是 @Get , @Path , @Operation , @ApiResponses ,和 @Parameters .

    简单地说,我如何才能告诉swagger我不需要请求主体?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Mateusz Kubuszok    7 年前

    如果您没有注释方法的某个参数,则会自动将其视为请求正文。如果您不想这样做,则必须显式地将其注释为其他内容,或者对其进行注释以忽略param @ApiParam(hidden = true) :

      // example usage of Swagger with Akka HTTP
      @ApiOperation(
        httpMethod = "GET",
        response   = classOf[ApiResponseX],
        value      = "docs"
      )
      @ApiImplicitParams(
        Array(
          new ApiImplicitParam(
            name      = "id",
            dataType  = "string",
            paramType = "query",
            value     = "id docs"
          )
        )
      )
      def fetchX(
        // even if this value has custom handling I have to explicitly ignore it,
        // to not make it into a request body
        @ApiParam(hidden = true) id: UUID
      ): Route = ...
    
    推荐文章