如何使用Swagger记录List <CustomObject>类型的Spring MVC请求参数

时间:2019-11-26 09:56:37

标签: java spring spring-mvc swagger

Spring Boot 2.1.8,Spring Web 5.1.9,Springfox Swagger 2.8.0,Swagger注释/模型1.5.14

我的RestController方法签名如下:

    @ApiOperation("List statuses")
    @GetMapping(produces = APPLICATION_JSON_VALUE)
    public ListResult<Status> listStatuses(
            @ApiParam("Filter given IDs")
            @RequestParam(value = "id", required = false, defaultValue = "") List<String> ids,
            @ApiParam(value = "Sort by property value", allowMultiple = true, format = "propertyName:<asc|desc>", type = "array")
            @RequestParam(value = "_sort", required = false, defaultValue = "") List<Sort> sorts
    )

ids如我所料-我可以输入多个单独的值:

ids

但是,无论我如何使用_sort的不同选项,@ApiParam始终被记录为单个字符串。有一个自定义的Spring Converter<String, Sort> bean被注册。

sort

如何强制Swagger将_sort参数作为字符串列表来处理,其中每个项目的格式必须为"propertyName:<asc|desc>"

1 个答案:

答案 0 :(得分:1)

使用 @ApiImplicitParam ,您可以将Swagger _sort参数强制为字符串列表

@ApiImplicitParam(name = "_sort", allowMultiple = true, dataType = "string", format = "propertyName:<asc|desc>", paramType = "query",
            value = "Sort by property value")