貓頭鷹的深夜翻譯:Spring REST服務異常處理
前言
這篇教程主要專注於如何優雅的處理WEB中的異常。雖然我們可以手動的設定ResponseStatus
,但是還有更加優雅的方式將這部分邏輯隔離開來。Spring提供了整個應用層面的異常處理的抽象,並且只是要求您新增一些註釋 - 它會處理其他所有內容。下面是一些程式碼的示例
如何手動處理異常
下面的程式碼中,DogController
將返回一個ResponseEntity
例項,該例項中包含返回的資料和HttpStatus
屬性
List<Dog> DogsNotFoundException DogServiceException
@RestController @RequestMapping("/dogs") public class DogsController { @Autowired private final DogsService service; @GetMapping public ResponseEntity<List<Dog>> getDogs() { List<Dog> dogs; try { dogs = service.getDogs(); } catch (DogsServiceException ex) { return new ResponseEntity<>(null, null, HttpStatus.INTERNAL_SERVER_ERROR); } catch (DogsNotFoundException ex) { return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND); } return new ResponseEntity<>(dogs, HttpStatus.OK); } }
這種處理異常的方式最大的問題就在於程式碼的重複。catch部分的程式碼在很多其它地方也會使用到(比如刪除,更新等操作)
Controller Advice
Spring提供了一種更好的解決方法,也就是Controller Advice。它將處理異常的程式碼在應用層面上集中管理。
現在我們的的DogsController的程式碼更加簡單清晰了:
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.NOT_FOUND; @ControllerAdvice public class DogsServiceErrorAdvice { @ExceptionHandler({RuntimeException.class}) public ResponseEntity<String> handleRunTimeException(RuntimeException e) { return error(INTERNAL_SERVER_ERROR, e); } @ExceptionHandler({DogsNotFoundException.class}) public ResponseEntity<String> handleNotFoundException(DogsNotFoundException e) { return error(NOT_FOUND, e); } @ExceptionHandler({DogsServiceException.class}) public ResponseEntity<String> handleDogsServiceException(DogsServiceException e){ return error(INTERNAL_SERVER_ERROR, e); } private ResponseEntity<String> error(HttpStatus status, Exception e) { log.error("Exception : ", e); return ResponseEntity.status(status).body(e.getMessage()); } }
-
handleRunTimeException
:這個方法會處理所有的RuntimeException
並返回INTERNAL_SERVER_ERROR
狀態碼 -
handleNotFoundException
: 這個方法會處理DogsNotFoundException
並返回NOT_FOUND
狀態碼。 -
handleDogsServiceException
: 這個方法會處理DogServiceException
並返回INTERNAL_SERVER_ERROR
狀態碼
這種實現的關鍵就在於在程式碼中捕獲需檢查異常並將其作為RuntimeException
丟擲。
還可以用@ResponseStatus
將異常對映成狀態碼
@ControllerAdvice public class DogsServiceErrorAdvice { @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler({DogsNotFoundException.class}) public void handle(DogsNotFoundException e) {} @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler({DogsServiceException.class, SQLException.class, NullPointerException.class}) public void handle() {} @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler({DogsServiceValidationException.class}) public void handle(DogsServiceValidationException e) {} }
在自定義的異常上新增狀態碼
@ResponseStatus(HttpStatus.NOT_FOUND) public class DogsNotFoundException extends RuntimeException { public DogsNotFoundException(String message) { super(message); } }