@ControllerAdvice public class ExceptionAdvice { // 處理業(yè)務(wù)異常 @ExceptionHandler(BusinessException.class) public ResponseEntity handaleException(BusinessException be){ // 打印 be.printStackTrace(); // 拿到業(yè)務(wù)異常對(duì)象 ErrorResult errorResult = be.getErrorResult(); // 返回信息 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult.getErrMessage()); } // 處理其他異常 @ExceptionHandler(Exception.class) public ResponseEntity handleOthers(Exception e){ e.printStackTrace(); // 返回系統(tǒng)異常 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error()); } }
本文摘自 :https://www.cnblogs.com/