前端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注解就可以了

相关推荐
hahaha 1hhh5 小时前
中文乱码 ubuntu autodl
linux·运维·前端
棉猴5 小时前
Python海龟绘图之绘制文本
javascript·python·html·write·turtle·海龟绘图·输出文本
计算机学姐5 小时前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app
Codebee5 小时前
Harness Engineering:AICode 的灵魂
前端·人工智能·前端框架
Highcharts.js6 小时前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
hpysirius6 小时前
在企业搭建一套完整的AI Agent系统
前端
追逐梦想永不停6 小时前
记录一个好用的excel判断数字格式的公式
前端·chrome·excel
hpysirius6 小时前
从零构建 Web 端视频剪辑器:技术实践与思考
前端
让学习成为一种生活方式6 小时前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
李白的天不白6 小时前
请求不到百度网址的原因
前端