List<String> 属性的 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

有什么方法可以为 List 属性生成正确的示例值吗?

更新 :

我已经尝试了@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 票证

原文由 Anil Bharadia 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
1 个回答

我设法让它工作,生成一个字符串列表。

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

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

这是我的例子:

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

当我渲染 swagger 页面时,我得到以下输出:

 "addLines": [
      "AddLine1",
      "AddLine2",
      "AddLine3",
      "AddLine4"
    ],

原文由 dane_griffiths 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题