用于api响应的Swagger doc(类型列表)

mtr*_*ebi 9 java swagger

我正在使用swagger来生成我的REST API的文档.但是,我在指定某些API调用的响应时遇到问题.

这是我的代码:

@GET
@Path("/self/activities")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all activities created by this user",
            notes = "Returns the list that the authenticated user   (JWT) has created",
            response = Activity.class,
            responseContainer = "List")
@ApiResponses(value = {
      @ApiResponse(code = 400, response = ErrorResponse.Error.class, responseContainer = "List", message = "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)"),
      @ApiResponse(code = 500, response = ErrorResponse.Error.class, responseContainer = "List", message = "Unknown Internal server error")})
public void getActivities(...){...}
Run Code Online (Sandbox Code Playgroud)

这是它生成的文档:

"responses" : {
      "200" : {
        "description" : "successful operation",
        "schema" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Activity"
          }
        }
      },
      "400" : {
        "description" : "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)",
        "schema" : {
          "$ref" : "#/definitions/Error"
        }
      },
      "500" : {
        "description" : "Unknown Internal server error",
        "schema" : {
          "$ref" : "#/definitions/Error"
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么错误响应不像200响应那样是'Array'类型.我想要的是所有响应的格式与200响应(带有项目的数组)相同:

 "500" : {
        "description" : "Uknown interval server error",
        "schema" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Error"
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)

谢谢你的时间.

Squ*_*lex 8

@ApiResponse(code = 400, response = ErrorResponse.Error.class, responseContainer = "List", message = "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)"),
  @ApiResponse(code = 500, response = ErrorResponse.Error.class, responseContainer = "List", message = "Unknown Internal server error")})
Run Code Online (Sandbox Code Playgroud)

400和500错误响应将返回ErrorResponse.不是数组.

  • 但它应该是一个数组,因为我指定了 `responseContainer = "List"`。而且我很确定这是这样做的方法,因为如果我以这种格式将 200 响应放在“@ApiResponses”中,它就可以工作。 (2认同)