Springboot(統一異常處理)
阿新 • • 發佈:2019-02-06
@ControllerAdvice public class ExceptionHandle { private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle. class ); @ExceptionHandler (value = Exception. class ) @ResponseBody public Result handle(Exception e){ if (e instanceof CourseException){ CourseException courseException = (CourseException) e; return ResultUtil.error(courseException.getCode(),courseException.getMessage()); } logger.error( "系統異常={}" ,e); return ResultUtil.error( 0 ,e.getMessage()); } } |
public class CourseException extends RuntimeException { private Integer code; public CourseException(ResultEnum resultEnum){ super (resultEnum.getMsg()); this .code = resultEnum.getCode(); } public Integer getCode() { return code; } public void setCode(Integer code) { this .code = code; } } |