0
我目前使用播放2.3路径片段,我有处理类似URL映射:玩! - 如何重用公共逻辑
GET /api/companyId/employeeId/tasks
GET /api/companyId/employeeId/timesheets
etc.
在每一个得到我需要执行类似的逻辑:
public Promise<Result> getEmployeeTimesheets(Long companyId, Long employeeId) {
return promise(() -> {
if (!companyRepository.one(companyId).isPresent()) {
return notFound("Company doesn't exist");
}
if (!employeeRepository.one(employeeId).isPresent()) {
return notFound("Employee doesn't exist");
}
if (!employeeRepository.employeeWorksForCompany(companyId, employeeId)) {
return forbidden("Employee doesn't work for this company");
}
// some actual logic here
});
}
此代码重复一遍又一遍。到目前为止,我使用普通的旧继承并将该重复代码移动到父控制器类中。它完成了工作,但它肯定不是完美的解决方案(因为我必须调用父方法并在每个控制器操作中手动检查结果)。
Play中是否有一些声明式方法会自动处理URL的片段(在我们的例子中为/api/companyId/employeeId
),并将执行委托给适当的控制器或返回错误响应(例如404 - 未找到)。
我最终使用@With方法功能之前被调用,但有轻微的警告;我必须手动解析URL的路径参数: - / – Xorty 2015-02-24 12:51:56