You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.3.3中ExceptionHandler返回的ModelAndView无法渲染

Spring Boot 3.3.3 过滤器链异常处理失效问题

问题描述

在Spring Boot 3.3.3环境下,尝试通过默认HandlerExceptionResolver捕获安全过滤器链中的异常,交由@ControllerAdvice的@ExceptionHandler统一处理。日志显示异常已进入处理方法,但返回的ModelAndView未生效,浏览器最终显示空白页面。

测试场景

  • 实现SampleFilter:请求特定URI时抛出自定义SampleException
  • 实现FilterChainExceptionHandlerFilter:捕获过滤器链中的异常并传递给HandlerExceptionResolver,将这两个过滤器注册到SecurityFilterChain
  • 在@ControllerAdvice类中定义对应@ExceptionHandler方法,返回指向error视图的ModelAndView
  • 自定义ErrorController处理/error请求,对应Thymeleaf模板error.html

异常现象

  • 访问未触发异常的不存在URL(如http://localhost:8080/does-not-exist)时,错误页面可正常渲染
  • 访问触发SampleException的URL时,日志明确记录异常已被处理,但浏览器显示空白页面

复现步骤

  1. 克隆项目:git clone https://github.com/smaring/spring-servlet-filter-exception.git
  2. 进入项目目录:cd spring-servlet-filter-exception
  3. 启动应用:mvn clean spring-boot:run
  4. 分别访问http://localhost:8080/does-not-exist和触发异常的URL,对比页面表现

内容的提问来源于stack exchange,提问作者Steve Maring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:32:37