Arko
Arko

Reputation: 972

Use multiple Pageable parameters in Spring MVC endpoint

I have a situation like this

@Autowired
private CustomerRepository repo;

@RequestMapping("/")
public Page<Customer> getDocuments(@Qualifier("bar") Pageable pageable,
                                   @Qualifier("foo")Pageable pageable2)
{
    return repo.findAll(pageable,pageable2.getOffset(), pageable2.getPageSize());
}

but it did not work well. My question is, how can I distinguish between the parameter values.

To achieve the above scenario I had to change my method to this:

@RequestMapping("/")
public Page<Customer> getDocuments(Pageable pageable,
                                   @RequestParam(value="from", defaultValue="0") int offSet,
                                   @RequestParam(value="length", defaultValue="3") int length)
{
    return repo.findAll(pageable, offSet, length);
}

Upvotes: 5

Views: 3035

Answers (2)

Arko
Arko

Reputation: 972

Finally, I have found my answer. What I did was absolutely correct but I was passing the wrong parameters in my URL.

In short, if you are passing two Pageable there has to be a @Qualifier and you need to set your request parameter accordingly.

what I was passing before in URL http://localhost:8080/?page=0&size=2&page=0&size=3 and the correct URL would be http://localhost:8080/?bar_page=0&bar_size=2&foo_page=0&foo_size=3

PS: whoever downvoted my question I am still mad at him/them.

Upvotes: 8

sandeshch
sandeshch

Reputation: 11

I think it is wrong idea to pass Spring beans as argument to rest resource. If the values needs to be accessed from Bean, then it should access inside the resource assuming bean is already initialized.

`@Autowired private CustomerRepository repo;

@Autowired
@Qualifier("bar")
Pageable pageable;

@Autowired
@Qualifier("foo")
Pageable pageable2;

@RequestMapping("/")
 public Page<Customer> getDocuments(){

   return     repo.findAll(pageable,pageable2.getOffset(),pageable2.getPageSize());
}`

Upvotes: -1

Related Questions