后端报错进行拦截,不然直接暴露给前端,有风险

40 阅读1分钟

在springboot项目中加入这个全局异常处理类

import org.example.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        try{

        }
        catch(Exception exception){
            e.printStackTrace();
        }

    // Result.fail(msg); msg可以自由的替换成你想要的输出语句
        return Result.fail("操作失败");
    }
}