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

相关推荐
GISer_Jing16 分钟前
AI Agent中游产业链全景拆解:智能体开发的核心生态与技术版图
前端·人工智能·后端
前端之虎陈随易1 小时前
2年没用Nodejs了,Bun很香
linux·前端·javascript·vue.js·typescript
Hooray1 小时前
用时7天,花费30元,我vibe coding这个网站
前端·agent·ai编程
小小高不懂写代码1 小时前
RAG--检索增强生成--原理及实战
前端·人工智能
空中海2 小时前
04 工程化、质量体系与 React 生态
前端·ubuntu·react.js
好运的阿财2 小时前
OpenClaw工具拆解之host_workspace_write+host_workspace_edit
前端·javascript·人工智能·机器学习·ai编程·openclaw·openclaw工具
XiYang-DING2 小时前
JavaScript
开发语言·javascript·ecmascript
ffqws_3 小时前
Spring Boot 接收前端请求的四种参数方式
前端·spring boot·后端
空中海3 小时前
02 React Native状态、导航、数据流与设备能力
javascript·react native·react.js
是安迪吖3 小时前
企业资产管理系统练习
前端·ai