如何为Swagger API响应指定泛型类型 [英] How to specify a generic type class for Swagger API response

查看:16404
本文介绍了如何为Swagger API响应指定泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约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:{
//这里对象A有自己的模型
}
]
}

所以我有一个基类响应类,其类型T的泛型如下:

  public class Response< T> {
@ApiModelProperty(value =上次访问时间)
private String lastAccessed;
@ApiModelProperty(value =创建时的时间)
private String createdOn;
private String lastModified;
@ApiModelProperty(value =创建于)
private boolean isReadOnly;
@ApiModelProperty(value =有权访问该对象的用户。)
private List< String> usersAllowed;
private int noOfEntries;
私人T对象;

// getters and setters
}

API A返回类型为Object的字段,我在控制器中返回Response作为API响应:

 公共类A {
@ApiModelProperty(value =Name)
私有字符串名称;
@ApiModelProperty(value =OID)
私人字符串ID;
// getters and setters
}

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


有没有办法在swagger中递归地指定响应对象的模型?例如,我可以使用注释@ApiOperation(response = Response.class),但是没有A的模型。 解决方案

<



这可以通过 List

来完成,这是一个旧帖子,但对于寻找答案的任何人来说: code>, Set Map 响应对象,但任何其他具有泛型类型的类都将被忽略。如果您正在使用这三者中的任何一个,那么您可以在 responseContainer 字段中指定它们,并在响应中指定您的类型。

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


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

{
    "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:

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:

  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);

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.

解决方案

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 Map response 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 responseContainer field and your inferred type in the response field.

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

这篇关于如何为Swagger API响应指定泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆