有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

列表<String>属性的java swagger@ApiModelProperty示例值

我有一个类,其中有一个属性是List<String>

public class MyClass {
    ....
    @ApiModelProperty(position = 2)
    private List<String> productIdentifiers;
    ....
}

此代码生成如下示例值:

{
  "customerId": "1001",
  "productIdentifiers": [
    "string"
  ],
  "statuses": [
    "NEW"
  ]
}

此处显示的示例值无效。我期望的示例值应如下所示:

{
  "customerId": "1001",
  "productIdentifiers": [
    "PRD1",
    "PRD2",
    "PRD3"
  ],
  "statuses": [
    "NEW"
  ]
}

我尝试按如下方式传递示例属性,但它没有生成正确的值:

@ApiModelProperty(position = 2, example = "PRD1, PRD2, PRD3")
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" // Its not json array

@ApiModelProperty(position = 2, example = "[\"PRD1\", \"PRD2\", \"PRD3\"]")
// This generates -> "productIdentifiers": "[\"PRD1\", \"PRD2\", \"PRD3\"]" // Its too not json array

是否有任何方法可以为列表属性生成适当的示例值

更新:

我尝试了@nullpointer和@Zeeshan Arif建议的解决方案

@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3")
private List<String> productIdentifiers;
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`

更新2:

尝试了以下方法,未产生正确响应

@ApiModelProperty(position = 2, dataType="java.util.List<String>", example = "PRD1, PRD2, PRD3")
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3"


@ApiModelProperty(position = 2, dataType="String[]", example = "PRD1, PRD2, PRD3")
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3"

我对swagger jar的maven依赖性是:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
    <exclusions>
        <exclusion>
            <artifactId>mapstruct</artifactId>
            <groupId>org.mapstruct</groupId>
        </exclusion>
    </exclusions>
</dependency>

更新 github ticket for this issue


共 (6) 个答案

  1. # 1 楼答案

    这似乎不受Swagger API的支持。同时,您可以使用这个Springfox插件生成一个单例列表示例(单值列表)https://github.com/aaitmouloud/springfox-collection-example-plugin

    只需将此添加到您pom.xml

    <dependency>
        <groupId>com.github.aaitmouloud</groupId>
        <artifactId>springfox-collection-example-plugin</artifactId>
        <version>2.9.2</version>
    </dependency>
    

    并将正确的类导入到Spring上下文中

    @ComponentScan({"springfox.collection.example.plugins"})
    

    然后,您应该在属性上声明一个单值示例,插件会将其转换为单值列表示例(适用于所有java.util.Collection类)

    @ApiModelProperty(value ="my property description", example = "2019-12-20T12:00:00")
    @NotNull
    private List<LocalDateTime> dates;
    

    免责声明:我是这个插件的作者

  2. # 2 楼答案

    下面是一个对象列表的工作示例。大摇大摆版本2.9.2。所需要的只是将数据类型定义为“List”,并在swagger文档中呈现。查找随附图片中呈现的ProductAll列表enter image description here

    @ApiModel
    public class ProductGetAllDTO {
        @ApiModelProperty(example="20")
        private String count;
        @ApiModelProperty(dataType="List", value = "rows")
        private List<ProductAll> rows;
    }
    
  3. # 3 楼答案

    您只需使用Reflection表示法。使用

    @ApiModelProperty(dataType = "[Ljava.lang.String;")
    

    很好,但我不能举例说明

    结果是:

    {
      "field": [
        "string"
      ]
    }
    
  4. # 4 楼答案

    我成功地实现了这一点,生成了一个字符串列表

    在springfox 2的ApiModelProperty中,编写如下示例:

    example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]"
    

    以下是我的例子:

    @ApiModelProperty(value = "Address", name = "addLines", 
        example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]")
    

    渲染“招摇过市”页面时,会得到以下输出:

    "addLines": [
          "AddLine1",
          "AddLine2",
          "AddLine3",
          "AddLine4"
        ],
    
  5. # 5 楼答案

    尝试按如下方式初始化@ApiModelProperty

    public class MyClass {
        ....
        @ApiModelProperty(
            position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
        )
        private List<String> productIdentifiers;
        ....
    }
    
  6. # 6 楼答案

    TLDR:Swagger API的一位贡献者已经开发了此功能,将其添加到3.0.0版中,但还不确定何时发布。目前,它位于Swagger API GitHub的feature/3.0.0-rc2分支上

    我和斯威格一起工作了将近两个月,随着我们项目的进展,像这样的问题出现了。现在我做了一些研究,并在GitHub页面上阅读了Swagger API,发现这个功能根本不起作用

    如前所述here[这里将是另一个链接,但我的声誉不高,无法发布超过2个链接]自2015年8月以来,该功能已被多次请求,但运气不佳

    现在在this issue on the Swagger-API github上,一位撰稿人评论说:

    This takes a major refactoring of the models, which is on the way. 3 March 2017

    这导致了后来的评论:

    Will be supported in 3.0.0 support, please see the feature/3.0.0-rc2 branch for details. 27 June 2017

    2017年8月9日,有人问3.0.0版何时发布,没有进一步的回应

    因此,总之,对数组/列表示例的支持已经完成,应该在3.0.0版中提供,但没有更多关于何时发布的消息