我在Spring Boot应用程序中使用三层架构.我创建了3个包(model、service、controller),但我所做的是,service使用try-catch调用一个repo函数,然后在controller中调用它
例子:
服务:
public ResponseEntity<List<Customer>> getAllCustomers() {
try {
List<Customer> customers = new ArrayList<Customer>();
cutomerRepository.findAll().forEach(customers::add);
if (customers.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(customers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
控制器
@GetMapping("/viewList")
private ResponseEntity<?> getAllCustomers()
{
try{
return customerService.getAllCustomers();
}catch (Exception exception){
return new ResponseEntity<String>("Customers is not found", HttpStatus.METHOD_FAILURE);
}
}
对吗?我想我应该在没有任何其他逻辑或代码的情况下只提供customerRepository.findAll()
个服务,但我不确定.有什么 idea 吗?