javaspring-bootopenapispringdoc

How to set the default Pageable query params on Swagger UI?


I've been developing REST API endpoints with Spring Boot. Here's the controller class:

@RestController
@RequestMapping(
        path = "/api/products",
        produces = {MediaType.APPLICATION_JSON_VALUE})
@AllArgsConstructor
@Validated
public class ProductController {

    private ProductService productService;

    @GetMapping
    public ResponseEntity<PageDto<ProductDto>> getProducts(
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice,
            @PageableDefault(size = 100) Pageable pageable) {
        PageDto<ProductDto> products = productService.findProducts(minPrice, maxPrice, pageable);
        return ResponseEntity.status(HttpStatus.OK).body(products);
    }
    // other functions here
}

If I open http://localhost:8080/swagger-ui/index.html, here's what I see:

enter image description here

I have no idea how the default values of "Pageable" are set in this way. How can I set and/or hide the default values of "pageable"?


Solution

  • To fix the issue, you can add @ParameterObject which is adjacent to @PageableDefault in the binding method

    Here are the information of @ParameterObject and @PageableDefault

    @ParameterObject: This annotation (from org.springdoc.api.annotations) tells Springdoc to treat Pageable as a single object with query parameters (page, size, sort) and ensures proper rendering in Swagger UI.

    @PageableDefault: Specifies default values (e.g., page = 0, size = 100). These defaults should now be reflected in Swagger UI.

    Here is the code shown below

    @GetMapping
        public ResponseEntity<PageDto<ProductDto>> getProducts(
                @RequestParam(required = false) BigDecimal minPrice,
                @RequestParam(required = false) BigDecimal maxPrice,
                @ParameterObject
                @PageableDefault(size = 100, page = 0) Pageable pageable) {
            PageDto<ProductDto> products = productService.findProducts(minPrice, maxPrice, pageable);
            return ResponseEntity.status(HttpStatus.OK).body(products);
        }
    

    I hope it can help you fix the issue.