我正在尝试使用Swagger UI来创建和部署我的文档以及我用Spring Boot编写的API。我知道Swagger提供了一些注释来帮助在实际的控制器类中编写文档,但我在尝试让它们做我需要的事情时遇到了麻烦。
我的问题是我有一个通用的DTO类,每次调用我的API都会返回这个类。这个DTO有一个泛型对象的contents字段。如果我直接使用这些对象,我知道我可以使用如下内容
@ApiResponse(responseCode = "200", description = "Customer found",
    content = @Content(
        schema = @Schema(implementation = Customer.class)))为了给出对象的JSON表示应该是什么样子的规范。但是,因为我将所有内容包装在一个特定的ResponseDTO类中,所以我需要一种方法来指定contents字段应该是什么样子,并且我不确定可以使用哪些注释来实现这一点。我觉得肯定应该有像这样的东西
@ApiResponse(responseCode = "200", description = "Customer found",
    content = @Content(
        schema = @Schema(implementation = DTO.class, 
                         fields = { "contents" = @Schema(implementation = Customer.class)})))或者类似的东西。我还没能找到如何真正做到这一点的解释。我的直觉告诉我,应该有一种将模式放入模式中的方法,但也许还有另一种我没有考虑过的解决方案。任何帮助或寻找的方向都将是非常感谢的。提前谢谢。
发布于 2021-04-09 19:34:28
我知道这篇文章已经过时了,但我还是要写这篇文章,以防有人遇到类似的问题。
我还必须返回一个对象列表,其中这些对象的一个字段必须根据一些请求参数进行更改。为了在Swagger中记录这些子模式(如您所说的模式中的模式),您可以使用@Schema注释的oneOf attribute并提供泛型类的列表。
下面是一个示例:
Customer.java
public class Customer {
  // Some other fields...
  @Schema(
      description = "List of generic contents inside Customer",
      oneOf = {Bar.class, Foo.class})
  private List<IContent> contents;
}IContent.java只是一些接口(或者它可以是泛型类的任何超类):
public interface IContent extends Serializable { }假设Foo.java和Bar.java是您的两个泛型类:
public class Bar implements IContent {
  @Schema(description = "Some dummy name field inside Bar")
  private String dummyName;
  @Schema(description = "Some dummy number field inside Bar")
  private Integer dummyNo;
}
public class Foo implements IContent {
  @Schema(description = "Some dummy ID field inside Foo")
  private Long contentID;
}生成的Swagger UI:

如果您有不同于记录通用对象列表的不同需求,请参阅allOf and anyOf attributes了解其他类型的子模式属性。
发布于 2020-09-18 06:39:25
我成功地用@Schema标记了class的字段,它工作得很好,但我用来给map提供信息。
class CreateReq extends DTO {
    @Schema(description="",
        implementation = Products.class)
    Map<String,Map<String,Object>>  products;
}
    // For openapi document only
    private static class Products {
        ProductFields productId1;
        ProductFields productId2;
    }
    // For openapi document only
    private static class ProductFields {
        String field1;
        Object field2;
    }发布于 2020-09-18 05:46:22
看起来你是在倒退。为此,我使用http://swagger.io上的编辑器创建了一个.yaml文件,该文件指定了API
这样,您的.yaml文件可以为每种数据类型指定不同的DTO,并且您可以为每种API指定它返回的类型。
此外,您不会发布您的ResponseDTO类的样子。我希望你已经泛化了它:
public class ResponseDTO<T> {
  public T getData() { … }
  …
}但我不确定拥有这样的对象是不是一个好主意。在我过去的一些项目中,我们为每种数据类型定义了一个单独的响应类,但它们都做同样的事情!您的框架可能已经为您编写了类似的类。
您使用的是Spring Boot,这也是我使用的。对于Spring,这个类是org.springframework.http.ResponseEntity。(其他框架可能有自己的版本。)而且它是泛化的,所以您可以向其中添加任何数据类型的内容。但是Swagger要求您指定添加内容的类型,而不是ResponseEntity类本身。你的ResponseDTO类挡住了你的路。
因此,如果您希望返回一种类型的CustomProduct,您的swagger生成的注释将指定CustomProduct.class,并且您的服务方法将如下所示:
… (more annotations)
@ApiResponses(value = { 
  @ApiResponse(code=200, message="Found", response=CustomProduct.class)
})
public ResonseEntity<CustomProduct> getCustomProduct(…) {
  CustomProduct myCustomProduct = retrieveCustomProduct(…);
  return new ResponseEntity<>(myCustomProduct, HTTPStatus.OK);
}https://stackoverflow.com/questions/62943407
复制相似问题