代码如下
@RestControllerAdvice(annotations = RestController.class)
@Slf4j
public class GlobalExceptionInterceptor {
/**
* 捕获自定义异常,返回json信息
*/
@ExceptionHandler({MamchargeException.class})
@ResponseBody
public ResultVO errorHandle(MamchargeException e) throws Exception {
return new ResultVO(null,e.getErrorCode(),e.getMsg());
}
@ExceptionHandler({Exception.class})
@ResponseBody
public ResultVO errorHandle(Exception e) throws Exception {
log.error("系统出错:",e);
return ResultVO.isFail();
}
}
RestControllerAdvice 注解表示拦截那一层
ExceptionHandler 拦截什么异常类 后面的是自定义异常类