@ControllerAdvice public class ExceptionAdvice { // 处理业务异常 @ExceptionHandler(BusinessException.class) public ResponseEntity handaleException(BusinessException be){ // 打印 be.printStackTrace(); // 拿到业务异常对象 ErrorResult errorResult = be.getErrorResult(); // 返回信息 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult.getErrMessage()); } // 处理其他异常 @ExceptionHandler(Exception.class) public ResponseEntity handleOthers(Exception e){ e.printStackTrace(); // 返回系统异常 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error()); } }
本文摘自 :https://www.cnblogs.com/