Java List<String> 属性的 swagger @ApiModelProperty 示例值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40989038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swagger @ApiModelProperty example value for List<String> property
提问by Anil Bharadia
I have one class in which there is one property which is List<String>
我有一个班级,其中有一个属性是 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
Is there any way I can generate proper example value for List property ?
有什么方法可以为 List 属性生成正确的示例值吗?
Update :
更新 :
I have tried the solutions suggested by @nullpointer and @Zeeshan Arif
我已经尝试了@nullpointer 和@Zeeshan Arif 建议的解决方案
@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3")
private List<String> productIdentifiers;
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`
Update 2 :
更新2:
Tried following approach which did notgenerate 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"
my maven dependency for swagger jar is :
我对 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>
回答by Zeeshan Arif
Try to initialize @ApiModelProperty
as follows:
尝试初始化@ApiModelProperty
如下:
public class MyClass {
....
@ApiModelProperty(
position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
)
private List<String> productIdentifiers;
....
}
回答by Daniel Borges
You just use Reflection
notation. Using
您只需使用Reflection
符号。使用
@ApiModelProperty(dataType = "[Ljava.lang.String;")
works fine, but I can't put examples.
工作正常,但我不能举例子。
This is the result:
这是结果:
{
"field": [
"string"
]
}
回答by KoenC
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
TLDR:Swagger-API 的一位贡献者已经致力于在 3.0.0 版中添加此功能,但尚不确定何时发布。现在它位于 Swagger-API GitHub 的 feature/3.0.0-rc2 分支上
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).
我已经和 Swagger 一起工作了将近两个月,随着我们的项目进展,出现了这样的问题。现在我做了一些研究,并在 Swagger-API 的 GitHub 页面上阅读了这个功能根本不起作用(还)。
As described hereand [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.
如上所述这里和[这里将是一个其他的联系,但我的名声不够高后超过2个链接]自2015年8月这一功能已被要求多次与没有多少运气。
Now on this issue on the Swagger-API github, one of the contributors commented:
现在关于Swagger-API github上的这个问题,其中一位贡献者评论道:
This takes a major refactoring of the models, which is on the way. 3 March 2017
这需要对模型进行重大重构,而且正在进行中。2017 年 3 月 3 日
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
将在 3.0.0 支持中得到支持,详情请参见 feature/3.0.0-rc2 分支。2017 年 6 月 27 日
And on 9 August 2017someone asked when the release of version 3.0.0 would be with no further response.
并在2017年8月9日,有人问时的3.0.0版本的发布将是一个没有进一步的回应。
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.
所以总而言之,对数组/列表示例的支持已经在进行中,应该在 3.0.0 版中可用,但没有更多关于何时发布的消息。
回答by Helder Pereira
An ugly workaround until we have this feature properly supported, which produces examples for lists with only one element, but at least allows to show something more useful than just "string"
is by using allowableValues
:
在我们正确支持此功能之前,这是一个丑陋的解决方法,它为只有一个元素的列表生成示例,但至少允许显示比"string"
使用更有用的东西allowableValues
:
@ApiModelProperty(position = 2, allowableValues = "PRD1")
// This generates -> "productIdentifiers": ["PRD1"]
回答by dane_griffiths
I managed to get this to work, generating a List of Strings.
我设法让它起作用,生成了一个字符串列表。
Within the ApiModelProperty with springfox 2, write your example as follows:
在带有 springfox 2 的 ApiModelProperty 中,编写如下示例:
example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]"
Here is my example:
这是我的例子:
@ApiModelProperty(value = "Address", name = "addLines",
example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]")
When I render the swagger page, I get the following output:
当我呈现 swagger 页面时,我得到以下输出:
"addLines": [
"AddLine1",
"AddLine2",
"AddLine3",
"AddLine4"
],
回答by bpedroso
I changed my example to the code below and it worked.
我将我的示例更改为下面的代码并且它起作用了。
public class MyClass {
....
@ApiModelProperty(
position = 2, datatype="List", example = "'[''{''PRD1''}','{''PRD2''}'']"
)
private List<String> productIdentifiers;
....
}
回答by clapsus
This seems to not be supported by the Swagger API. In the mean time you can use this Springfox Plugin to generate a singleton list example (one value list) https://github.com/aaitmouloud/springfox-collection-example-plugin
Swagger API 似乎不支持这一点。同时,您可以使用此 Springfox 插件生成单例列表示例(一个值列表)https://github.com/aaitmouloud/springfox-collection-example-plugin
Just add this to you pom.xml
把这个加给你 pom.xml
<dependency>
<groupId>com.github.aaitmouloud</groupId>
<artifactId>springfox-collection-example-plugin</artifactId>
<version>2.9.2</version>
</dependency>
And import the right classes to your Spring context
并将正确的类导入到您的 Spring 上下文中
@ComponentScan({"springfox.collection.example.plugins"})
You should then declares a single value example on your property and it will be transformed to a singleton list example by the plugin (works for all java.util.Collection
classes)
然后,您应该在您的属性上声明一个单值示例,它将被插件转换为单例列表示例(适用于所有java.util.Collection
类)
@ApiModelProperty(value ="my property description", example = "2019-12-20T12:00:00")
@NotNull
private List<LocalDateTime> dates;
Disclaimer: I am the author of this plugin.
免责声明:我是这个插件的作者。
回答by tksilicon
Here is a working example for list of objects. Swagger version 2.9.2. All that is needed is for the dataType to define as "List" and it will render in the swagger documentation. Find attached the ProductAll listrendered in the attached picture.
这是对象列表的工作示例。Swagger 版本 2.9.2。所需要的只是将 dataType 定义为“List”,它将在 swagger 文档中呈现。找到附加图片中呈现的 ProductAll 列表。
@ApiModel
public class ProductGetAllDTO {
@ApiModelProperty(example="20")
private String count;
@ApiModelProperty(dataType="List", value = "rows")
private List<ProductAll> rows;
}
回答by Carlos Cavero
None of the solutions worked for me. As it is explained in this Baeldung articlebesides to include the Example Value in the data model with @ApiModelProperty
没有一个解决方案对我有用。正如这篇 Baeldung 文章中所解释的,除了在数据模型中包含示例值之外@ApiModelProperty
@ApiModel
public class Foo {
private long id;
@ApiModelProperty(name = "name", dataType = "List", example = "[\"str1\", \"str2\", \"str3\"]")
private List<String> name;
The Controller
must also be annotated with @ApiImplicitParams
to let Swagger point to the data model:
该Controller
还必须注解@ApiImplicitParams
让扬鞭点到数据模型:
@RequestMapping(method = RequestMethod.POST, value = "/foos")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "foo",
value = "List of strings", paramType = "body", dataType = "Foo") })
public Foo create(@RequestBody final Foo foo) {
You may notice that the dataType
point to the class Foo
.
您可能会注意到dataType
指向 class的点Foo
。