1、新建一个类,加上注解@RestControllerAdvice
2、方法上添加注解@ExceptionHandler(value = xxx.class)xxx为需要捕捉的类,可定义为自己写的异常类
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = ServiceException.class)
public AjaxResult exceptionHandler(Exception e) {
return AjaxResult.error(e.getMessage());
}
}