java 如何为 Swagger API 响应指定泛型类型类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33459644/
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 21:42:17  来源:igfitidea点击:

How to specify a generic type class for Swagger API response

javagenericsswaggerhttpresponse

提问by as3rdaccount

I have about 40 APIs that have similar base response structure as follows:

我有大约 40 个具有类似基本响应结构的 API,如下所示:

{
    "lastAccessed": "2015-30-08:14:21:45T",
    "createdOn": "2015-30-07:09:04:10T",
    "lastModified": "2015-30-08:14:21:45T",
    "isReadOnly": "false",
    "usersAllowed" : ["Tim", "Matt", "Christine"];
    "noOfEntries": 1,
    "object": [
        "ObjectA": {
             //here object A has its own model
         }
    ]
}

So I have a base response class taking a generic of type T as follows:

所以我有一个基本响应类,它采用 T 类型的泛型,如下所示:

public class Response<T> {
    @ApiModelProperty(value="Last time accessed")
    private String lastAccessed;
    @ApiModelProperty(value="Time when Created ")
    private String createdOn;
    private String lastModified;
    @ApiModelProperty(value="Created on")
    private boolean isReadOnly;
    @ApiModelProperty(value="Users that has access to the object.")
    private List<String> usersAllowed;
    private int noOfEntries;
    private T object;

    //getters and setters
}

So for the API A, which returns the Object of type with its own fields, I am returning Response as the API response in the controller:

因此,对于返回带有自己字段的对象类型的 API A,我将 Response 作为控制器中的 API 响应返回:

  public class A {
    @ApiModelProperty(value="Name")
    private String name;
    @ApiModelProperty(value="OID")
    private String id;    
    //getters and setters
}    

In the controller: Response data = new Response(); ResponseEntity response = new ResponseEntity<>(data, HttpStatus.OK);

在控制器中: Response data = new Response(); ResponseEntity response = new ResponseEntity<>(data, HttpStatus.OK);

Is there a way in swagger I can specify the model of the response object recursively? For example, I could have the annotation @ApiOperation(response=Response.class) but that would not have the model for A.

有没有办法在招摇我可以递归指定响应对象的模型?例如,我可以有注解 @ApiOperation(response=R​​esponse.class) 但那不会有 A 的模型。

回答by McLovin

I know this is an old post, but for anyone else looking for an answer to this:

我知道这是一篇旧帖子,但对于其他正在寻找答案的人来说:

This can be done for List, Set, and Mapresponse objects but any other class class with generic types will be ignored. If you are using any of these three, then you specify them in the responseContainerfield and your inferred type in the responsefield.

这可以针对ListSetMap响应对象完成,但任何其他具有泛型类型的类都将被忽略。如果您正在使用这三个中的任何一个,那么您可以在responseContainer字段中指定它们并在字段中指定您的推断类型response

@ApiResponse(code = 200, responseContainer="List", respone=java.lang.String.class)

回答by Supun Wijerathne

I am using swagger 2and following resolved this problem for me.

我正在使用swagger 2,以下为我解决了这个问题。

Remove 'response' property from both @ApiResponseand @ApiOperation. Then swagger will automatically generate the response class for you for 200 OKfrom method stubs (irrespective of whether with/without generics in response class).

从和 中删除“响应”属性。然后 swagger 会从方法存根自动为您生成200 OK的响应类(无论响应类中是否有/没有泛型)。@ApiResponse@ApiOperation

@ApiOperation(value = "what your operation does")

@ApiResponses(value = { @ApiResponse(code = 200, message = "Success message") })

Update: You can do this simple work around. Just say you want to output Response<MyClass>as response return type. You can do,

更新:你可以做这个简单的工作。只是说你想输出Response<MyClass>为响应返回类型。你可以做,

  • In controller class, specify an empty private class like this

    private MyResponseClass extends Response<MyClass>{}

  • And for the swagger spec, specify like this,

    @ApiResponse(code = 200, respone=MyResponseClass.class)

  • 在控制器类中,像这样指定一个空的私有类

    private MyResponseClass extends Response<MyClass>{}

  • 对于 swagger 规范,像这样指定,

    @ApiResponse(code = 200, respone=MyResponseClass.class)

Remember that at the moment, swagger doesn't support generics. Above two are just workarounds.

请记住,目前,swagger 不支持泛型。以上两个只是解决方法。