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

我有大约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


}




 public class A {


 @ApiModelProperty(value="Name")


 private String name;


 @ApiModelProperty(value="OID")


 private String id; 


 //getters and setters


} 



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

在swagger中是否有方法我可以递归地指定响应对象的模型?

时间:

对于List,Set和Map响应对象,可以这样做,但是泛型类型的类都会被忽略,如果你使用这三种方法中的一种,那么在responseContainer字段中指定它们,并在response字段中。


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



我使用swagger 2,下面的命令解决了这个问题。

从'response'属性中同时删除@ApiResponse和@ApiOperation,然后,swagger将自动生成200 ok的response类。


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



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



...