1

I'm curious to know how does Spring Boot work with Pageable in controllers methods? I add a Pageable parameter to my controller method and Spring handle it for me.

There is an example:

@GetMapping("search")
public ResponseEntity<List<ProductDTO>> searchProducts(
        @RequestParam(value = "query", required = false, defaultValue = "") String query,
        Pageable pageable) {
    Specification<Product> spec = Specification.where(null); 
    if(!StringUtils.isBlank(query)) {
        Node rootNode = new RSQLParser().parse(query);
        spec = rootNode.accept(new CustomRsqlVisitor<Product>());
    }
    Page<ProductDTO> page = service.searchProducts(spec, pageable);
    // My Additional code....
}

Now let's back to the main question: How does Spring handle Pageable? Then, how can I create the same business for myself to handle Specifications on input methods?

P.S. 1: I used rsql-parser to implement search over my products.

P.S. 2: You can find custom classes that are used to generate Specification under rsql package

3
  • 1
    You can output the HQL to a log file and check for yourself. Pageable will be implemented using LIMIT and OFFSET, or a similar approach, depending on the underlying SQL database. Commented Sep 29, 2021 at 9:09
  • I need to know how spring converts path parameters into a Peagable. I wanna do the same for the query
    – GLinBoy
    Commented Sep 29, 2021 at 9:24
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare stackoverflow.com/help/self-answer
    – Yunnosch
    Commented Oct 3, 2021 at 17:57

2 Answers 2

1

You can do this by implementing a custom HandlerMethodArgumentResolver.

Creating a HandlerMethodArgumentResolver:

@Component
public class CustomClassMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().equals(CustomClass.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
            NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        // You can get the values you need from the request and convert it to a new object.
        // Example: String requestPathInfo = nativeWebRequest.getRequest().getPathInfo();
        // Example: String headerUrl = nativeWebRequest.getHeader("X-Url");
        return new CustomClass(/*...*/);
    }
}

Register the HandlerMethodArgumentResolver:

You can have to make our CustomClassMethodArgumentResolver known to Spring Boot.

@Configuration
public class WebConfig implements WebMvcConfigurer {

    //...

    @Override
    public void addArgumentResolvers(
      List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CustomClassMethodArgumentResolver());
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.