问题描述
我使用 Spring Boot 开发了一个微服务.REST API 的文档是用 Swagger 制作的.一些 REST 资源利用 Spring 概念免费提供分页.下面是一个例子:
@RequestMapping(value = "/buckets", method = GET)public PagedResources list(Pageable pageable, PagedResourcesAssembler 汇编器) {return bucketService.listBuckets(pageable, assembler);}
如果我打开 Swagger 页面,资源可以使用以下表单:
我遇到的问题是使用 content-type application/json 检测到 pageable 参数,例如,我不知道如何传递值来更改页面大小.所有值似乎都被忽略了.
是否可以将查询参数作为 JSON 对象传递?或者是否可以配置 Swagger 为 Pageable 接口包含的 getter 生成独立的查询参数字段?
请注意,我将 Springfox 与 Gradle 一起使用:
编译'io.springfox:springfox-spring-web:2.3.1'编译'io.springfox:springfox-swagger2:2.3.1'编译'io.springfox:springfox-swagger-ui:2.3.1'
这是 Spring-Fox 的一个已知问题.请参阅问题 https://github.com/springfox/springfox/issues/755
2 https://github.com/springfox/springfox/issues/755#issuecomment-135059871
I have developed a microservice using Spring Boot. The documentation for the REST API is made with Swagger. Some REST resources make use of Spring concepts to provide pagination for free. Below is an example:
@RequestMapping(value = "/buckets", method = GET)
public PagedResources list(Pageable pageable, PagedResourcesAssembler assembler) {
return bucketService.listBuckets(pageable, assembler);
}
If I open the Swagger page, the following form is available for the resource:
The issue I have is that the pageable parameter is detected with content-type application/json and I don't know how to pass a value to change the page size for example. All values seem to be ignored.
Is it possible to pass the query parameters as JSON object? or is it possible to configure Swagger to generate independent query parameter fields for getters contained by the Pageable interface?
Please note that I am using Springfox with Gradle:
compile 'io.springfox:springfox-spring-web:2.3.1'
compile 'io.springfox:springfox-swagger2:2.3.1'
compile 'io.springfox:springfox-swagger-ui:2.3.1'
This is a known issue with Spring-Fox. See Issue #755. Based on zdila's comment 2 at this time alternative is to add @ApiImplicitParams which is not ideal but it does work.
@ApiImplicitParams({
@ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
value = "Results page you want to retrieve (0..N)"),
@ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
value = "Number of records per page."),
@ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
value = "Sorting criteria in the format: property(,asc|desc). " +
"Default sort order is ascending. " +
"Multiple sort criteria are supported.")
})
[
1 https://github.com/springfox/springfox/issues/755
2 https://github.com/springfox/springfox/issues/755#issuecomment-135059871
这篇关于Spring Pageable 接口的 Swagger 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2