I have one class in which there is one property which is List
public class MyClass {
....
@ApiModelProperty(position = 2)
None of the solutions worked for me. As it is explained in this Baeldung article besides to include the Example Value in the data model with @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:
@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
.
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;
....
}
I managed to get this to work, generating a List of Strings.
Within the ApiModelProperty with springfox 2, write your example as follows:
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:
"addLines": [
"AddLine1",
"AddLine2",
"AddLine3",
"AddLine4"
],
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 list rendered in the attached picture.
@ApiModel
public class ProductGetAllDTO {
@ApiModelProperty(example="20")
private String count;
@ApiModelProperty(dataType="List", value = "rows")
private List<ProductAll> rows;
}
Try to initialize @ApiModelProperty
as follows:
public class MyClass {
....
@ApiModelProperty(
position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
)
private List<String> productIdentifiers;
....
}
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
:
@ApiModelProperty(position = 2, allowableValues = "PRD1")
// This generates -> "productIdentifiers": ["PRD1"]