前端vue报net::ERR_FAILED 500 (Internal Server Error)

spring boot的filter抛出的自定义异常,使用自定义controller来捕获。

复制代码
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 捕获filter抛出的异常交由BusinessExceptionAdvice处理
 * 请求进来 会按照 filter -> interceptor -> controllerAdvice -> aspect -> controller的顺序调用
 * 当controller返回异常 也会按照controller -> aspect -> controllerAdvice -> interceptor -> filter来依次抛出
 *
 * @author dengdxu
 * @Datetime 2024/8/24
 **/
@Controller
public class ErrorControllerImpl implements ErrorController {

    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public void handleError(HttpServletRequest request) throws Throwable {
        if (request.getAttribute("javax.servlet.error.exception") != null) {
            throw (Throwable) request.getAttribute("javax.servlet.error.exception");
        }
    }
}

其他业务异常正常返回,但是filter异常在前端报跨域异常:No 'Access-Control-Allow-Origin' header is present, net::ERR_FAILED 500 (Internal Server Error)

这是由于自定义的controller没有配置跨域,给ErrorControllerImpl 加上@CrossOrigin注解就可以了

相关推荐
kyriewen117 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
摇滚侠8 小时前
JAVA 项目教程《苍穹外卖-12》,微信小程序项目,前后端分离,从开发到部署
java·开发语言·vue.js·node.js
Timer@9 小时前
LangChain 教程 04|Agent 详解:让 AI 学会“自己干活“
javascript·人工智能·langchain
阿珊和她的猫9 小时前
TypeScript中的never类型: 深入理解never类型的使用场景和特点
javascript·typescript·状态模式
skywalk81639 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan11 小时前
FastAPI -API Router的应用
前端·网络·python
走粥12 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory00112 小时前
layui select重新渲染
前端·layui
weixin1997010801612 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化