Swagger文档针对API响应(类型为List)

10
我正在使用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(...){...}

这是它生成的文档:

"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"
        }
      }
    }

我不明白为什么错误响应不像200响应那样是“数组”类型的。我的目标是让所有响应都具有与200响应相同的格式(带项目的数组):

 "500" : {
        "description" : "Uknown interval server error",
        "schema" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Error"
          }
        }
      }

谢谢您抽出时间。


你在这里使用的swagger-core版本是哪个? - Ron
1个回答

11
@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")})

400和500错误响应将返回一个ErrorResponse。而不是一个数组。


2
但它应该是一个数组,因为我正在指定 responseContainer = "List"。而且我非常确定这是正确的方法,因为如果我按照这种格式将200响应放在 '@ApiResponses' 中,它就可以工作。 - mtrebi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接