本文主要是介绍前端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注解就可以了
这篇关于前端vue报net::ERR_FAILED 500 (Internal Server Error)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!