Search code examples
springswagger-2.0springfox

Automatically adding @ImplicitParams with specific type of method argument of Spring Controller


Previously, I had Spring controller as below.

@RequestMapping(method = GET)
public List<TheResponse> getResponses(
    @RequestParam(defaultValue = "1") int offset, 
    @RequestParam(defaultValue = "10") int limit) {

    Pagination pagination = new Pagination(offset, limit);
    ...
    return someResponse;
}

Swagger was generating document of this method with correct parameters information.

Later I created PaginationArgResolver by implementing HandlerMethodArgumentResolver. After that, I have to update the method as below, and apply @ApiImplicitParams to make it work with Swagger.

@ApiImplicitParams({ 
    @ApiImplicitParam(name = "offset", dataType = "int", defaultValue = "1"),
    @ApiImplicitParam(name = "limit", dataType = "int", defaultValue = "10")
})
@RequestMapping(method = GET)
public List<TheResponse> getResponses(@ApiIgnore Pagination pagination) {

    ...
}

Is there anyway @ImplicitParams is applied automatically whenever Pagination type argument is found?

OR

If I expose @PaginationSupported annotation, can I process it to achieve same results?

I am currently using springfox v2.4.0.

PS. I can edit source of Pagination class, in case some swagger annotation is needed there.


Solution

  • Why adding @ApiIgnore springfox will resolve these attributes inside the class automatically. When you want to add default values and other stuff you can add the @ApiParam annotation to the class attributes as well.

    class Pagination {
        @ApiParam(defaultValue = "1")
        private int offset;
        // [..]
    }