招摇 @ApiModelProperty List<String> 的示例值财产

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

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

I have one class in which there is one property which is List<String>

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

此代码生成的示例值如下:

This code generates the example values as following:

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

此处显示的示例值无效.我预期的示例值应该是这样的:

The example values here shown are not valid. My expected example values should be like :

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

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

I have tried passing example attribute as following but it is not generating proper value:

@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 属性生成正确的示例值吗?

Is there any way I can generate proper example value for List property ?

更新:

我已经尝试过@nullpointer 和@Zeeshan Arif 建议的解决方案

I have tried the solutions suggested by @nullpointer and @Zeeshan Arif

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

更新 2:

尝试了以下没有产生正确响应的方法

Tried following approach which did not generate proper response

@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 依赖项是:

my maven dependency for swagger jar is :

<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票

推荐答案

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

I managed to get this to work, generating a List of Strings.

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

Within the ApiModelProperty with springfox 2, write your example as follows:

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

这是我的例子:

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

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

When I render the swagger page, I get the following output:

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

相关文章