swagger中@ApiModelProperty中example属性对List的支持
swagger中@ApiModelProperty注解example使用问题
example属性默认是String的, 对应List的支持不好
@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
@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3")
private List<String> productIdentifiers;
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`
@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"
依赖版本
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
暂时还没有适配
TLDR: One of the contributers on Swagger-API has worked on this functionality to add this in version 3.0.0 but it's not sure yet when this will be released. For now it stands on the feature/3.0.0-rc2 branch at the Swagger-API GitHub
I've been working with Swagger for almost two months now and as our project progressed issues like this showed up. Now I did some research and read on the GitHub pages for the Swagger-API that this feature simply doesn't work (yet).
As described here and [here would be an other link but my reputation is not high enough to post more than 2 links] this feature has been requested several times since August 2015 with not much luck.
Now on this issue on the Swagger-API github, one of the contributors commented:
This takes a major refactoring of the models, which is on the way. 3 March 2017
which lead to a later comment:
Will be supported in 3.0.0 support, please see the feature/3.0.0-rc2 branch for details. 27 June 2017
And on 9 August 2017 someone asked when the release of version 3.0.0 would be with no further response.
So in conclusion, support for examples for arrays/Lists has been worked on and should be available in version 3.0.0 but no more news on when that would be released.
取巧的解决方法
# 此方法只能显示一个
public class MajorNutrientElements {
@ApiModelProperty(name = "majorName", value = "食材名称", example = "西红柿", required = true)
private String majorName;
@ApiModelProperty(name = "nutrientElements", value = "食材对应的营养元素", allowableValues = "维生素E", required = true)
private List<String> nutrientElements;
}
swagger中显示
{
"majorName": "西红柿",
"nutrientElements": [
"维生素E"
]
}
如果不写example和allowableValues, 如下展示
{
"majorName": "西红柿",
"nutrientElements": [
"string"
]
}
未验证,据说可以解决的
@ApiModelProperty(value = "Address", name = "addLines", dataType = "List",
example = "[AddLine1,AddLine2,AddLine3,AddLine4]")
渲染出来:
"addLines": [
"AddLine1",
"AddLine2",
"AddLine3",
"AddLine4"
]
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger 2 UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger 2 JSR-303 based documentation -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
站在巨人肩膀上摘苹果
https://www.it1352.com/972727.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端