Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表的swagger@ApiModelProperty示例值<;字符串>;财产_Java_Swagger_Springfox - Fatal编程技术网

Java 列表的swagger@ApiModelProperty示例值<;字符串>;财产

Java 列表的swagger@ApiModelProperty示例值<;字符串>;财产,java,swagger,springfox,Java,Swagger,Springfox,我有一个类,其中有一个属性是List 此处显示的示例值无效。我期望的示例值应如下所示: { "customerId": "1001", "productIdentifiers": [ "PRD1", "PRD2", "PRD3" ], "statuses": [ "NEW" ] } 我尝试按如下方式传递示例属性,但它没有生成正确的值: @ApiModelProperty(position = 2, example = "PRD1, PRD2,

我有一个类,其中有一个属性是
List

此处显示的示例值无效。我期望的示例值应如下所示:

{
  "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
是否有任何方法可以为列表属性生成适当的示例值

更新:

我尝试了@nullpointer和@Zeeshan Arif建议的解决方案

@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3")
private List<String> productIdentifiers;
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"`
@ApiModelProperty(position=2,dataType=“List”,example=“PRD1,PRD2,PRD3”)
私有列表标识符;
//这将生成->`“产品标识符”:“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"
@ApiModelProperty(position=2,dataType=“java.util.List”,example=“PRD1、PRD2、PRD3”)
//这将生成->“产品标识符”:“PRD1、PRD2、PRD3”
@ApiModelProperty(position=2,dataType=“String[]”,example=“PRD1、PRD2、PRD3”)
//这将生成->“产品标识符”:“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>

伊奥·斯普林福克斯

尝试初始化
@ApiModelProperty
,如下所示:

public class MyClass {
    ....
    @ApiModelProperty(
        position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
    )
    private List<String> productIdentifiers;
    ....
}
example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]"
公共类MyClass{
....
@ApiModelProperty(
位置=2,数据类型=“列表”,示例=“PRD1、PRD2、PRD3”
)
私有列表标识符;
....
}

您只需使用
反射
符号。使用

@ApiModelProperty(dataType = "[Ljava.lang.String;")
很好,但我不能举例说明

结果是:

{
  "field": [
    "string"
  ]
}

TLDR:Swagger API的一位贡献者已经开发了此功能,将其添加到3.0.0版中,但还不确定何时发布。目前,它位于Swagger API GitHub的feature/3.0.0-rc2分支上

我和斯威格一起工作了将近两个月,随着我们项目的进展,像这样的问题出现了。现在我做了一些研究,并在GitHub页面上阅读了Swagger API,发现这个功能根本不起作用

如前所述,[这里将是另一个链接,但我的声誉不足以发布超过2个链接]自2015年8月以来,该功能已被多次请求,但运气不佳

现在,其中一位撰稿人评论道:

这需要对模型进行重大重构,这一过程正在进行中。2017年3月3日

这导致了后来的评论:

将在3.0.0支持中受支持,有关详细信息,请参阅功能/3.0.0-rc2分支。2017年6月27日

2017年8月9日,有人问3.0.0版何时发布,没有进一步的回应


总之,对数组/列表示例的支持已经完成,应该在3.0.0版中提供,但没有更多关于何时发布的消息。我成功地实现了这一点,生成了一个字符串列表

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

public class MyClass {
    ....
    @ApiModelProperty(
        position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
    )
    private List<String> productIdentifiers;
    ....
}
example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]"
以下是我的例子:

@ApiModelProperty(value = "Address", name = "addLines", 
    example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]")
渲染“招摇过市”页面时,会得到以下输出:

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

我将我的示例更改为下面的代码,它成功了

public class MyClass {
....
@ApiModelProperty(
    position = 2, datatype="List", example = "'[''{''PRD1''}','{''PRD2''}'']"
)
private List<String> productIdentifiers;
....
}
公共类MyClass{
....
@ApiModelProperty(
position=2,datatype=“List”,example=“”['''{''PRD1'}','{''PRD2'}']”
)
私有列表标识符;
....
}

这似乎不受Swagger API的支持。同时,您可以使用这个Springfox插件生成一个单例列表示例(单值列表)

只需将此添加到您的
pom.xml


com.github.aaitmoound
springfox集合示例插件
2.9.2
并将正确的类导入到Spring上下文中

@ComponentScan({“springfox.collection.example.plugins”})
然后,您应该在属性上声明一个单值示例,插件会将其转换为单值列表示例(适用于所有
java.util.Collection
类)

@ApiModelProperty(value=“我的属性描述”,示例=“2019-12-20T12:00:00”)
@NotNull
私人名单日期;

免责声明:我是这个插件的作者。

下面是一个对象列表的工作示例。大摇大摆版本2.9.2。所需要的只是将数据类型定义为“List”,并在swagger文档中呈现。查找附加图片中呈现的ProductAll列表

@ApiModel
public class ProductGetAllDTO {
    @ApiModelProperty(example="20")
    private String count;
    @ApiModelProperty(dataType="List", value = "rows")
    private List<ProductAll> rows;
}
@ApiModel
公共类ProductGetAllDTO{
@ApiModelProperty(示例=“20”)
私有字符串计数;
@ApiModelProperty(dataType=“List”,value=“rows”)
私有列表行;
}

没有一个解决方案适合我。如中所述,使用
@ApiModelProperty

@ApiModel
public class Foo {
    private long id;
    @ApiModelProperty(name = "name", dataType = "List", example = "[\"str1\", \"str2\", \"str3\"]")
    private List<String> name;

您可能会注意到,
数据类型
指向V3中的类
Foo

,您可以省略数据类型定义和示例值。Swagger将根据数据类型生成示例。
列表将呈现为[{…YourCustomObject Properties…}]

@Anil bhardia对于
dataType=List“
”您得到了什么响应?另外,为了精确测试,您使用了什么样的招摇过市版本/依赖关系?@nullpointer我得到了“PRD1、PRD2、PRD3”“为了我所有的努力,@AnilBharadia你解决了吗?我正在与同样的问题作斗争,现在在Springfox 3.0.0中,提供的答案中没有一个不适合我。另请参见和@HonzaZidek。我在该项目上工作了这么久。据我记忆所及,我没能解决这个问题。我试过一些答案,但没有试过最近提供的答案。我将尝试创建一个示例项目来测试新答案是否有效。我已更新了原始问题中的详细信息。如果您需要更多详细信息,请告诉我。Springfox 3.0.0不适用,我刚刚测试过。不会生成JSON数组
[…]
,而是生成字符串
“[…]”@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) {