根据Optional的值定义要调用的方法

问题描述 投票:0回答:1

是否有可能以某种方式更改下面的代码,因此,如果未提供status值以返回存储在数据库中的所有PaymentLog对象,而不是仅返回status404相等的对象?基本上,我想如果没有提供status变量来调用服务层中的另一个方法logService.getAllPaymentLogs()

@GetMapping(Endpoints.LOGS.PAYMENT_LOGS)
public Page<PaymentLog> getPaymentLog(@RequestParam Optional<Integer> status) {
    return logService.getPaymentLogStatus(status.orElse(404), PageRequest.of(0, 10));
}

这些是getPaymentLogStatus()getAllPaymentLogs

@Override
public Page<PaymentLog> getPaymentLog(Pageable pageable) {
    return paymentLogRepository.getAllBy(pageable);
}

@Override
public Page<PaymentLog> getPaymentLog(int status, Pageable pageable) {
    return paymentLogRepository.getAllByStatus(status, pageable);
}
java spring spring-boot pagination option
1个回答
1
投票

@@ 123在评论会话中回答了问题:

status.map(s -> getPaymentLog(s, page)).orElseGet(() -> getPaymentLog(page))
© www.soinside.com 2019 - 2024. All rights reserved.