apimodelproperty爆红_swagger中@ApiModelProperty中example属性对List的支持

本文讨论了在Swagger中使用@ApiModelProperty注解时,example属性对List类型支持不佳的问题,并介绍了现有版本的限制及预计在3.0.0版本中的改进。同时,提供了一种通过设置allowableValues临时解决的方法,展示了如何在Swagger UI中正确渲染List类型的示例。
摘要由CSDN通过智能技术生成

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 productIdentifiers;

//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`

@ApiModelProperty(position = 2, dataType="java.util.List", 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"

依赖版本

io.springfox

springfox-swagger2

2.7.0

暂时还没有适配

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 nutrientElements;

}

swagger中显示

{

"majorName": "西红柿",

"nutrientElements": [

"维生素E"

]

}

如果不写example和allowableValues, 如下展示

{

"majorName": "西红柿",

"nutrientElements": [

"string"

]

}

36a69aacf600e42ef3c133d7a39a3b07.png

未验证,据说可以解决的

@ApiModelProperty(value = "Address", name = "addLines", dataType = "List",

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

渲染出来:

"addLines": [

"AddLine1",

"AddLine2",

"AddLine3",

"AddLine4"

]

io.springfox

springfox-swagger2

2.9.2

io.springfox

springfox-swagger-ui

2.9.2

io.springfox

springfox-bean-validators

2.9.2

站在巨人肩膀上摘苹果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值