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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31387334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:31:33  来源:igfitidea点击:

Swagger doc for api response (type List)

javaswagger

提问by mtrebi

I am using swagger to generate the documentation of my REST API. However I am having problems specifying the responses of some API calls.

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

This is my code:

这是我的代码:

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

And this is the doc that it generates:

这是它生成的文档:

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

And I do not understand why the error responses are not of type 'Array' like the 200 Response. What I want is that all the responses have the same format as the 200 responses (array with items):

而且我不明白为什么错误响应不像 200 响应那样属于“数组”类型。我想要的是所有响应都具有与 200 个响应(包含项目的数组)相同的格式:

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

Thanks for your time.

谢谢你的时间。

回答by Squalex

@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 and 500 errors response will return an ErrorResponse. Not an array.

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