Reputation: 1989
I'm trying to implement filtering and pagination to my spring boot application and I've ran into an issue.
This is my controller: page and size have a default value and every other parameter should be optional like below.
@PreAuthorize("hasAuthority('service_manager')")
@RequestMapping(path = "/clients", method = RequestMethod.GET)
public Page<ClientResponse> getClients(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String companyName,
@RequestParam(required = false) BigInteger firmRegNo,
@RequestParam(required = false) String address,
@RequestParam(required = false) BigInteger contractNo,
@RequestParam(required = false) BigInteger monthlyPay,
@RequestParam(required = false) User.UserStatus status
) {
ClientListRequest request = new ClientListRequest(companyName, firmRegNo, address, contractNo, monthlyPay, status);
return clientService.getAllClients(request, of(page, size, Sort.Direction.DESC))
.map(ClientResponse::new);
}
When making a GET request to "http://localhost:8080/clients?companyName=Selver" I'm getting this response:
{
"timestamp": "2019-02-18T07:40:44.182+0000",
"status": 500,
"error": "Internal Server Error",
"message": "At least one property must be given!",
"path": "/clients"
}
The same response comes without the parameters too. Any ideas?
Upvotes: 3
Views: 6364
Reputation: 1989
Found the mistake:
return clientService.getAllClients(request, of(page, size, Sort.Direction.DESC))
.map(ClientResponse::new);
needs at least one property after Sort.Direction.DESC
, which to sort by by default. I used "createTime" of a client to do so and got it to work!
return clientService.getAllClients(request, of(page, size, Sort.Direction.DESC, "createTime"))
.map(ClientResponse::new);
Upvotes: 10