Swagger @ApiOperation 可以允许在 Java 中指定列表列表吗?

2022-01-22 00:00:00 swagger swagger-ui java

我在 Java 类中有一个方法,其签名如下所示,我想为其添加 Swagger Rest 文档.

I have a method in a Java class with signature like below and I want to add Swagger Rest documentation for it.

public List<List<MyCustomClass>> getMyCustomClasses(String id){
//
}

我想知道是否有办法让 response 或 responseContainer 返回 List 对象列表?像下面这样的?

I want to know if there is a way to have either response or responseContainer to return List of List objects? Something like below?

@ApiOperation(value = "find MyCustomClass objects by id", response =MyCustomClass.class , responseContainer =   "List<List>")

如果方法的响应只是 List,我在生成 swagger 文档时没有问题我可以指定 response =MyCustomClass.class , responseContainer = "List" 但只有当它是 List of List 作为返回类型时才会出现问题.

I have no issues in generating swagger docs if the response of the method is just List where I could specify response =MyCustomClass.class , responseContainer = "List" but having problem only if it is List of List as return type.

谢谢

拉维

推荐答案

为此,您可以简单地创建一个模板类,如下所示:

In order to do this you can simply create a template class like this:

public class TempClass {
   private List<someClass> listOfClass;
   public List<someClass> getListOfClass() {
      return listOfClass;
   }
   public void setListOfClass(List<someClass> listOfClass) {
      this.listOfClass = listOfClass;
   }
}

或者更复杂的是:

public class TempClass {
   private List<List<List<someClass>>> listOfList;
   public List<List<List<someClass>>> getListOfList(){ 
      return listOfList;
   }
   public void setListOfList(List<List<List<someClass>>> listOfList) {
      this.listOfList = listOfList;
   }
}

最后得到这个类作为对方法上方注释的响应,如下所示:

And at the end get this class as a response to the annotation above the method like this:

@ApiOperation(response = TempClass.class)

您还应该将方法的返回类型指定为 TempClass

you should also specify the return type of the method as the TempClass

另一个问题讨论了相同的问题这里

There is another issue that discussed a same issue here

相关文章