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'
Vineet Bhatia's answer with
@ApiImplicitParams
looks fine. But I faced with situation, when@ApiIgnor
and@ApiParam(hidden = true)
doesn't work and you can still observe the asembler and pageable params. I fixed this problem by adding next lineto the Docket bean in my
SwaggerConfig
.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.
[
1 https://github.com/springfox/springfox/issues/755
2 https://github.com/springfox/springfox/issues/755#issuecomment-135059871
Building on Vineet Bhatia's answer, you can wrap the solution up in a custom annotation for reusability:
Which can then be used like so:
Answer to validation problem indicated by Evgeny.
Using
throws an exception:
(at least, it does with springfox-swagger2 and springfox-swagger2-ui version 2.9.2)
You can avoid the exception by following Evgeny's answer or by adding default values and example values for the integer parameters:
Answer of Vineet Bhatia will have validation problem when you are not running on localhost. It will argue for integer parameters that they are not corresponding to json schema.
So I changed integer to string:
This solution works without the need to annotate every single API method in every single controller. First we create a replacement for
Pageable
class with the correct property names and descriptions (Kotlin code, you can use a Interface for Java):Then in the Swagger config, just add a direct substitute from
Pageable
to this class (again Kotlin code, but Java should be pretty similar):The result looks like this:
The downside is not being able to define the default value in the
ApiModelProperty
, but this is more than good enough for my project.