我在 Spring Boot 應用程式中使用 3 層架構。我創建了 3 個包(模型、服務、控制器),但我所做的是,服務用 try catch 呼叫了一個 repo 函式,然后我在控制器中呼叫它
例子:
服務:
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(),但我不確定。任何想法?
uj5u.com熱心網友回復:
服務層應該包含邏輯,這樣就可以了。
但它不應包含來自控制器層的任何類,因為這會將資訊從“上”層泄漏到“下”層。這意味著您的服務不應回傳 ResponseEntity,因為它來自控制器層。相反,它應該只回傳一個客戶串列,并讓控制器從中構造 ResponseEntity。
否則,您的服務將始終被限制為由該特定控制器呼叫。它不能被不使用 HTTP ResponseEntity 的不同型別控制器的另一個服務呼叫。
uj5u.com熱心網友回復:
我認為最好的方法如下。
您的服務層不應ResponseEntity<List<Customer>>像當前那樣回傳。它應該改為回傳List<Customer>。
這已經在上面的答案中,但想回答以進一步擴展內容。
該服務還應在修改為回傳時List<Customer>處理具有應用程式特定例外的例外。因此,您為您的應用程式創建自己的例外、此例外的模型以及您創建一個Exception Advice class以通用方式處理所有這些應用程式例外的位置。因此,您的服務只會拋出例外,控制器不會捕獲它,它將由 Advice 類(用 注釋@ControllerAdvice)處理,該類將處理所有未捕獲的例外并回傳適當的回應。在 Spring 中還有更多選項可以以通用方式處理例外。
我附上以下代碼作為示例
處理從控制器冒出的所有例外的類。
@ControllerAdvice
public class ErrorHandler {
@ExceptionHandler(ApplicationException.class)
public ResponseEntity handleApplicationException(ApplicationException e) {
return ResponseEntity.status(e.getCustomError().getCode()).body(e.getCustomError());
}
}
一些特定于應用程式的例外(名稱可能更具體)
@Getter
@Setter
public class ApplicationException extends RuntimeException {
private CustomError customError;
public ApplicationException(CustomError customError){
super();
this.customError = customError;
}
}
發生例外時回傳給客戶端的 Error 物件
@Getter
@Setter
@NoArgsConstructor
public class CustomError {
private int code;
private String message;
private String cause;
public CustomError(int code, String message, String cause) {
this.code = code;
this.message = message;
this.cause = cause;
}
@Override
public String toString() {
return "CustomError{"
"code=" code
", message='" message '\''
", cause='" cause '\''
'}';
}
}
您的服務
public List<Customer> getAllCustomers() {
try {
List<Customer> customers = new ArrayList<Customer>();
cutomerRepository.findAll().forEach(customers::add);
if (customers.isEmpty()) {
throw new ApplicationException(new CustomError(204, "No Content", "Customers do not exist"));
}
return new ResponseEntity<>(customers, HttpStatus.OK);
} catch (Exception e) {
throw new ApplicationException(new CustomError(500, "Server Error", "Disclose to the client or not what the cause of the error in the server was"));
}
}
它自己的控制器還可以檢查它接收到的輸入資訊,如果需要,它可以自己拋出一個特定于應用程式的例外,或者只回傳一個適當的回應,其中包含輸入中的錯誤資訊。
這樣控制器只是處理用戶和服務層之間的輸入/輸出。
該服務只是處理來自持久層的資料的輸入/輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/495511.html
