如何为 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": {
//here object A has its own model
}
]
}
因此,我有一个基本响应类,采用 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
}
因此,对于 API A,它返回具有自己字段的 Object 类型,我将响应作为控制器中的 API 响应返回:
public class A {
@ApiModelProperty(value="Name")
private String name;
@ApiModelProperty(value="OID")
private String id;
//getters and setters
}
在控制器中:响应数据 = 新的响应();ResponseEntity response = new ResponseEntity<>(data, HttpStatus.OK);
有没有办法在 swagger 中以递归方式指定响应对象的模型?例如,我可以将注释@ApiOperation(response=Response.class),但该注释将没有A的模型。