案例类中的查询参数-Swagger文档

时间:2018-10-16 08:13:07

标签: scala spring-boot refactoring swagger openapi

我正在为我的应用程序编写API。 该应用程序允许管理员搜索具有查询参数的用户。

Image of all the query params

我的问题

我的控制器中有很多查询参数正在占用大量空间。目前,我已经通过将所有QueryParameters作为方法中的参数来解决了这个问题,

def findUser (
            @ApiParam(name = "firstName", value = "First name", `type` = "query")
            @RequestParam(value = "firstName", required = false) firstName: String,

            @ApiParam(name = "surname", value = "Surname", `type` = "query")
            @RequestParam(value = "surname", required = false) surname: String,

            @ApiParam(name = "email", value = "Email adress", `type` = "query")
            @RequestParam(value = "email", required = false) email: String,

            @ApiParam(name = "telephoneNumber", value = "Phone number", `type` = "query")
            @RequestParam(value = "telephoneNumber", required = false) telephoneNumber: String,

            @ApiParam(name = "landlineNumber", value = "Land line number", `type` = "query")
            @RequestParam(value = "landlineNumber", required = false) landlineNumber: String,

            @ApiParam(name = "dateOfBirth", value = "Date of Birth", `type` = "query")
            @RequestParam(value = "dateOfBirth", required = false) dateOfBirth: String,

            @ApiParam(name = "postalAddress", value = "Postal adress", `type` = "query")
            @RequestParam(value = "postalAddress", required = false) postalAddress: String,

            @ApiParam(name = "postCode", value = "Post code", `type` = "query")
            @RequestParam(value = "postCode", required = false) postCode: String
) = { 
// doing stuff
}

问题:

是否有一种方法可以将所有查询参数提取到Swagger Docs中的类或对象中?如下图所示:

def allQueryParamsInACaseClass (
  queryParams : AllQueryParams
)

1 个答案:

答案 0 :(得分:2)

我使用@ModelAttribute进行了修复,然后使用@BeanProperties创建了一个案例类。

def allQueryParamsInACaseClass ( 
  @ModelAttribute queryParams: AllQueryParams
)

case class AllQueryParams (
  ...
  @ApiParam(name = "firstName", value = "First name", `type` = "query")
  @RequestParam(value = "firstName", required = false) @BeanProperty firstName: String,
  ..
)