新建一个类作为全局异常处理器:

@RestControllerAdvice //必要标签
public class GlobalExceptionHandler {

    //全局异常处理器
    @ExceptionHandler(Exception.class) //处理的异常种类
    public Result ex(Exception e){
        e.printStackTrace();//打印堆栈信息
        return Result.error("操作错误");
    }

}

---------------------------