gpt4 book ai didi

java - List 属性的 swagger @ApiModelProperty 示例值

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:46 24 4
gpt4 key购买 nike

我有一个类,其中有一个属性是 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 ticket for this issue

最佳答案

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

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

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

这是我的例子:

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

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

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

关于java - List<String> 属性的 swagger @ApiModelProperty 示例值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com