swagger @ApiModelProperty example value for List property

前端 未结 9 2349
温柔的废话
温柔的废话 2020-12-14 00:10

I have one class in which there is one property which is List

public class MyClass {
    ....
    @ApiModelProperty(position = 2)
         


        
相关标签:
9条回答
  • 2020-12-14 00:41

    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.

    0 讨论(0)
  • 2020-12-14 00:45

    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;
    ....
    }
    
    0 讨论(0)
  • 2020-12-14 00:46

    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"
        ],
    
    0 讨论(0)
  • 2020-12-14 00:48

    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;
    }
    
    0 讨论(0)
  • 2020-12-14 00:55

    Try to initialize @ApiModelProperty as follows:

    public class MyClass {
        ....
        @ApiModelProperty(
            position = 2, datatype="List", example = "PRD1, PRD2, PRD3"
        )
        private List<String> productIdentifiers;
        ....
    }
    
    0 讨论(0)
  • 2020-12-14 00:58

    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"]
    
    0 讨论(0)
提交回复
热议问题