SpringMVC源码-异常处理机制

定义一个异常处理类TestErrorController:

java 复制代码
@Controller
public class TestErrorController {
  
    @RequestMapping("/exception")
    public ModelAndView exception(ModelAndView view) throws ClassNotFoundException {
        view.setViewName("index");
        throw new ClassNotFoundException("class not found");
    }

    @RequestMapping("/nullpointer")
    public ModelAndView nullpointer(ModelAndView view) {
        view.setViewName("index");
        String str = null;
        str.length();
        return view;
    }  
            
    @ExceptionHandler(RuntimeException.class)
    public ModelAndView error(RuntimeException error, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("msg", "Runtime error");
        return mav;
    }

    @ExceptionHandler()
    public ModelAndView error(Exception error, HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("msg", "Exception error");
        return mav;
    }
}

访问请求:http://localhost:8080/spring_mymvc/nullpointer

开始调用

// 执行调用准备调用controller里的实际方法

调用执行目标方法的链路;

bash 复制代码
nullpointer:22, TestErrorController (com.mashibing.controller.exception)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
doInvoke:202, InvocableHandlerMethod (org.springframework.web.method.support)
invokeForRequest:143, InvocableHandlerMethod (org.springframework.web.method.support)
invokeAndHandle:109, ServletInvocableHandlerMethod (org.springframework.web.servlet.mvc.method.annotation)
invokeHandlerMethod:929, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handleInternal:825, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handle:90, AbstractHandlerMethodAdapter (org.springframework.web.servlet.mvc.method)
doDispatch:1127, DispatcherServlet (org.springframework.web.servlet)
doService:1006, DispatcherServlet (org.springframework.web.servlet)
processRequest:1085, FrameworkServlet (org.springframework.web.servlet)
doGet:960, FrameworkServlet (org.springframework.web.servlet)
service:655, HttpServlet (javax.servlet.http)
service:945, FrameworkServlet (org.springframework.web.servlet)
service:764, HttpServlet (javax.servlet.http)
internalDoFilter:227, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilter:53, WsFilter (org.apache.tomcat.websocket.server)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
invoke:197, StandardWrapperValve (org.apache.catalina.core)
invoke:97, StandardContextValve (org.apache.catalina.core)
invoke:541, AuthenticatorBase (org.apache.catalina.authenticator)
invoke:135, StandardHostValve (org.apache.catalina.core)
invoke:92, ErrorReportValve (org.apache.catalina.valves)
invoke:687, AbstractAccessLogValve (org.apache.catalina.valves)
invoke:78, StandardEngineValve (org.apache.catalina.core)
service:360, CoyoteAdapter (org.apache.catalina.connector)
service:399, Http11Processor (org.apache.coyote.http11)
process:65, AbstractProcessorLight (org.apache.coyote)
process:890, AbstractProtocol$ConnectionHandler (org.apache.coyote)
doRun:1789, NioEndpoint$SocketProcessor (org.apache.tomcat.util.net)
run:49, SocketProcessorBase (org.apache.tomcat.util.net)
runWorker:1191, ThreadPoolExecutor (org.apache.tomcat.util.threads)
run:659, ThreadPoolExecutor$Worker (org.apache.tomcat.util.threads)
run:61, TaskThread$WrappingRunnable (org.apache.tomcat.util.threads)
run:748, Thread (java.lang)

抛出异常:





获取玩参数然后调用具体的方法返回模型视图

处理返回值;

然后对模型视图进行渲染页面就会显示;

相关推荐
云烟成雨TD8 分钟前
Spring AI Alibaba 1.x 系列【5】ReactAgent 构建器深度源码解析
java·人工智能·spring
最初的↘那颗心12 分钟前
Agent 实战:构建第一个 Agent 与记忆系统设计
java·大模型·agent·spring ai·记忆系统
程序猿_极客13 分钟前
SpringBoot 三大参数注解详解:@RequestParam @RequestBody @PathVariable 区别及常用开发注解
java·spring boot·后端·面试八股文·springboot注释
小胖java17 分钟前
校园通衢公告枢纽系统
java·spring boot
Crazy________18 分钟前
docker4.8
java·开发语言·eureka
T__TIII20 分钟前
milvus 数据备份和还原
后端
cch891822 分钟前
Laravel 2.x:早期框架的奠基之路
java·开发语言
用户9623779544827 分钟前
代码审计 | Filter —— Tomcat 内存马从零到注入
后端
snakeshe101028 分钟前
从装饰器到动态代理:彻底理解 Java AOP 的底层原理与实战应用
后端
小飞Coding29 分钟前
基于 Redis +Lua+ ZooKeeper 的轻量级内嵌式限流
后端