java - 用于 api 响应的 Swagger 文档(类型列表)

标签 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(...){...}

这是它生成的文档:

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

感谢您的宝贵时间。

最佳答案

@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。不是数组。

关于java - 用于 api 响应的 Swagger 文档(类型列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31387334/

相关文章:

java - Eclipse 构建路径 - 声明的包不匹配

rest - 如何确认我的 API 中使用的 swagger 版本?

flutter - 如何使用 token 来实现授权功能?

c# - 使用容器实例在 Azure 中未启用访问 Swagger

java - 嵌套循环,如何制作日历

java - android logcat 消息在 eclipse 中不可见

java - 使用 OCR 检测扫描文档是否创建 PDF [pdfbox]

swagger - 如何更改 Swagger Codegen 生成的 JavaScript 模块名称?

rest - 与 Swagger 相比,使用 Spring REST Docs 有什么好处

java - PopupWindow 适用于 Android 2.1,但不适用于其他版本