我正在整理自己的分类资料,并且遇到了障碍。我可以按特定字段排序,但方向仍然是硬编码。
@PreAuthorize("hasAuthority('service_manager')")
@RequestMapping(path = "/clients", method = RequestMethod.GET)
public Page<ClientResponse> getClients(
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "10") Integer 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,
@RequestParam(defaultValue = "createTime") String sort,
@RequestParam(defaultValue = "desc") String sortDir
) {
ClientListRequest request = new ClientListRequest(companyName, firmRegNo, address, contractNo, monthlyPay, status);
return clientService.getAllClients(request, of(page, size, Sort.Direction.DESC, sort))
.map(ClientResponse::new);
}
如何根据Sort.Direction.DESC
的参数值交替使用Sort.Direction.ASC
和sortDir
?
我尝试过:
if (sortDir == "asc") {
return clientService.getAllClients(request, of(page, size, Sort.Direction.ASC, sort))
.map(ClientResponse::new);
} else {
return clientService.getAllClients(request, of(page, size, Sort.Direction.DESC, sort))
.map(ClientResponse::new);
}
不起作用,看起来也不正确...
答案 0 :(得分:0)
尝试这样的事情:
Pageable pageable = new PageRequest(page, size, Sort.Direction.fromString(sortDir), sort);
return repository.findAll(pageable);
答案 1 :(得分:0)
我用了这个:
Sort.Direction direction;
if (sortDir.equals("asc")) {
direction = Sort.Direction.ASC;
} else {
direction = Sort.Direction.DESC;
}
return clientService.getAllClients(request, of(page, size, direction, sort))
.map(ClientResponse::new);