【问题标题】:Spring REST Pageable / Query Parameters in SwaggerSwagger 中的 Spring REST 可分页/查询参数
【发布时间】:2019-11-06 18:22:24
【问题描述】:

我正在尝试在我的 REST 服务中实现分页。

采取 1:

public Page<Item> getPagedItems(Pageable pageable)

这是 SpringFox / Swagger 中的一个已知错误,其中 Swagger 页面显示错误的参数名称。另外,我只想要页面和大小选项。

采取 2:

public Page<Item> getPagedItems(@RequestParam(name="page", required=true) int page, @RequestParam(name="page", required=true) int size)

这给了我正确的参数,但它不允许我设置参数描述或示例值。

采取 3:

@ApiImplicitParams({
    @ApiImplicitParam(name="page", value="page description", required=true, example="0", dataType="int"),
    @ApiImplicitParam(name="size", value="size description", required=true, example="10", dataType="int")
})
public Page<Item> getPagedItems(int page, int size)

这还有另一个错误,其中 example="0" 不起作用,但除此之外的所有其他值都有效,哈哈。如果我将数据类型更改为字符串,那么它将显示 0 示例值,但随后它会让您在文本框中输入任何内容。还尝试了“0”、“0.0”、“0\0”等。

如果我不设置示例,那么 Springfox 会抛出一个异常,抱怨没有示例大声笑。我能做的最接近的事情是将两者都设置为 0 并留空。啊。 0 / 10 不是一个好选择,因为 0 不起作用,但 10 可以。

知道如何让 0 的示例起作用吗? Spring Fox 似乎没有得到很好的支持,即使是开源的,所以在 GitHub 上没有回应。

或者其他方式有一个例子和描述?

【问题讨论】:

    标签: java swagger swagger-ui springfox


    【解决方案1】:

    我们创建了一个继承的注解并使用了它:

     @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page",  dataTypeClass = int.class,defaultValue = "0", example = "0", paramType = "query", value = "Results page you want to retrieve (0..N)"),
        @ApiImplicitParam(name = "size", dataTypeClass = int.class ,example = "10", paramType = "query", value = "Number of records per page."),
        @ApiImplicitParam(name = "sort", allowMultiple = true, dataTypeClass=String.class, paramType = "query", value = "Sorting criteria in the format: property(,asc|desc). "
                + "Default sort order is ascending. " + "Multiple sort criteria are supported.")  
    })
    public @interface ApiPagingParams {
    
    }
    

    用法

    @ApiPagingParams
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/entity") 
    ResponseEntity<Page<...>> getSurveyEntityList(@ApiIgnore("see @ApiPagingParams") @PageableDefault(page = 0, size = 10) Pageable pageable,...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 2015-12-28
      • 1970-01-01
      • 2020-03-03
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      相关资源
      最近更新 更多