Ani*_*dia 38 java swagger springfox
我有一个类,其中有一个属性 List<String>
public class MyClass {
....
@ApiModelProperty(position = 2)
private List<String> productIdentifiers;
....
}
Run Code Online (Sandbox Code Playgroud)
此代码生成示例值,如下所示:
{
"customerId": "1001",
"productIdentifiers": [
"string"
],
"statuses": [
"NEW"
]
}
Run Code Online (Sandbox Code Playgroud)
此处显示的示例值无效.我期望的示例值应该是:
{
"customerId": "1001",
"productIdentifiers": [
"PRD1",
"PRD2",
"PRD3"
],
"statuses": [
"NEW"
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试将示例属性传递如下,但它没有生成正确的值:
@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
Run Code Online (Sandbox Code Playgroud)
有什么办法可以为List属性生成适当的示例值吗?
更新:
我尝试过@nullpointer和@Zeeshan Arif建议的解决方案
@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3")
private List<String> productIdentifiers;
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`
Run Code Online (Sandbox Code Playgroud)
更新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"
Run Code Online (Sandbox Code Playgroud)
我对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>
Run Code Online (Sandbox Code Playgroud)
更新 此问题的github票证
小智 15
TLDR:Swagger-API的贡献者之一已经开发了这个功能,可以在3.0.0版本中添加它,但它还不确定何时发布.目前它位于Swagger-API GitHub的feature/3.0.0-rc2分支上
我已经和Swagger一起工作了近两个月,随着我们项目的进展,这样的问题出现了.现在我做了一些研究并阅读了Swagger-API的GitHub页面,这个功能根本不起作用.
如上所述这里和[这里将是一个其他的联系,但我的名声不够高后超过2个链接]自2015年8月这一功能已被要求多次与没有多少运气.
现在关于Swagger-API github的这个问题,其中一个贡献者评论道:
这需要对模型进行重大的重构,这正在进行中.2017年3月3日
这导致后来的评论:
将支持3.0.0支持,详情请见功能/ 3.0.0-rc2分支.2017年6月27日
并在2017年8月9日,有人问时的3.0.0版本的发布将是一个没有进一步的回应.
总而言之,对数组/列表的示例的支持已经开始工作,并且应该在3.0.0版本中提供,但是没有关于何时发布的新闻.
小智 10
我设法让它工作,生成一个字符串列表.
在ApiModelProperty中,将数据类型定义为List,并按如下方式编写示例:
example = "[AddLine1,AddLine2,AddLine3,AddLine4]"
Run Code Online (Sandbox Code Playgroud)
这是我的例子:
@ApiModelProperty(value = "Address", name = "addLines", dataType = "List",
example = "[AddLine1,AddLine2,AddLine3,AddLine4]")
Run Code Online (Sandbox Code Playgroud)
当我渲染swagger页面时,我得到以下输出:
"addLines": [
"AddLine1",
"AddLine2",
"AddLine3",
"AddLine4"
],
Run Code Online (Sandbox Code Playgroud)
小智 7
您只需使用Reflection符号。使用
@ApiModelProperty(dataType = "[Ljava.lang.String;")
Run Code Online (Sandbox Code Playgroud)
工作正常,但我不能举例子。
这是结果:
{
"field": [
"string"
]
}
Run Code Online (Sandbox Code Playgroud)
尝试@ApiModelProperty按如下方式初始化:
public class MyClass {
....
@ApiModelProperty(
position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
)
private List<String> productIdentifiers;
....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43688 次 |
| 最近记录: |