• SpringMVC异常处理


    1 概述

    SpringMVC 框架处理异常的常用方式:使用@ExceptionHandler 注解处理异常。
    
    • 1

    2 @ExceptionHandler注解和用@ControllerAdvice注解

    2.1 @ExceptionHandler注解

    	使用注解@ExceptionHandler 可以将一个方法指定为异常处理方法。该注解只有一个可
    选属性 value,为一个 Class数组,用于指定该注解的方法所要处理的异常类,即所要匹
    配的异常。
    	而被注解的方法,其返回值可以是 ModelAndView、String,或 void,方法名随意,方法
    参数可以是 Exception 及其子类对象、HttpServletRequest、HttpServletResponse 等。系统会
    自动为这些方法参数赋值。
    	对于异常处理注解的用法,也可以直接将异常处理方法注解于 Controller 之中。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2 @ControllerAdvice注解

    	注解@ControllerAdvice,字面理解就是“控制器增强”,是给控制器对象增强
    功能的。使用@ControllerAdvice 修饰的类中可以使用@ExceptionHandler。
    	当使用@RequestMapping 注解修饰的方法抛出异常时,会执行@ControllerAdvice 修饰的
    类中的异常处理方法。@ControllerAdvice 是使用@Component 注解修饰的,可以
    扫描到@ControllerAdvice 所在的类路径(包名),创建对象。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.实战演示

    (1)定义异常类
    	定义三个异常类:NameException、AgeException、MyUserException。其中 MyUserException
    是另外两个异常的父类。
    
    • 1
    • 2
    • 3

    MyUserException

    public class MyUserException extends Exception{
    
        public MyUserException() {
            super();
        }
    
        public MyUserException(String message) {
            super(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    AgeException

    public class AgeException extends MyUserException{
    
        public AgeException() {
        }
    
        public AgeException(String message) {
            super(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    NameException

    public class NameException extends MyUserException{
        public NameException() {
        }
    
        public NameException(String message) {
            super(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    (2)定义异常响应页面
    
    • 1

    ageErrorPage

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>ageExceptiontitle>
    head>
    <body>
    ageErrorPage<br>
    <hr>
    ${ex.message}
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    nameErrorPage

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>nameErrortitle>
    head>
    <body>
    nameErrorPage<br>
    <hr>
    ${ex.message}
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    defaultErrorPage

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>defaultPagetitle>
    head>
    <body>
    defaultErrorPage<br>
    <hr>
    ${ex.message}
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    (3)前端页面代码
    
    • 1
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
    %>
    <html>
    <head>
        <title>主页title>
        <base href="<%=path%>">
    head>
    <body>
    <form action="some.do" method="post">
        姓名:<input type="text" name="name"><br>
        年龄:<input type="text" name="age"><br>
        <input type="submit" value="提交">
    form>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    (4)控制器代码
    
    • 1
    @Controller
    public class MyController {
    
        @RequestMapping(value = "/some.do",method = RequestMethod.POST)
        public ModelAndView testException(String name,Integer age) throws NameException, AgeException {
            ModelAndView mv = new ModelAndView();
            if (!"zs".equals(name)){
                throw new NameException("名称不正确");
            }
            if (age != 18){
                throw new AgeException("年龄不正确");
            }
    
            mv.addObject("name",name);
            mv.addObject("age",age);
            mv.setViewName("show");
            return mv;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    (5)全局异常处理类代码
    
    • 1
    /**
     * @ControllerAdvice
     * 处理器类发生异常可以到该类中寻找@ExceptionHandler
     */
    
    @ControllerAdvice
    public class GlobalExceptionResolver {
    
        @ExceptionHandler(NameException.class)
        public ModelAndView nameExceptionResolver(NameException nameException){
            ModelAndView mv = new ModelAndView();
            mv.addObject("ex",nameException);
            mv.setViewName("nameException");
            return mv;
        }
    
        @ExceptionHandler(AgeException.class)
        public ModelAndView ageExceptionResolver(AgeException ageException){
            ModelAndView mv = new ModelAndView();
            mv.addObject("ex",ageException);
            mv.setViewName("ageException");
            return mv;
        }
    
        @ExceptionHandler
        public ModelAndView defaultExceptionResolver(Exception exception){
            ModelAndView mv = new ModelAndView();
            mv.addObject("ex",exception);
            mv.setViewName("defaultException");
            return mv;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    (6)配置文件disPatcher类代码
    
    • 1
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.hkd.springmvc.controller"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    (7)测试1
    在这里插入图片描述
    在这里插入图片描述
    测试2
    在这里插入图片描述
    在这里插入图片描述

    容易忽视的点
    @ControllerAdvice 是使用@Component 注解修饰的,可以< context:component-scan>
    扫描到@ControllerAdvice 所在的类路径(包名),创建对象。

  • 相关阅读:
    极智开发 | 讲解 Nginx 特性之三:动静分离
    Ph.D,指哲学博士学位
    ThinkPHP5远程代码执行高危漏洞
    docker搭建jenkins
    SpringMVC的文件上传&文件下载&多文件上传---详细介绍
    区块链(9):java区块链项目的Web服务实现之实现web服务
    《LC刷题总结》—— 二叉树
    从根儿上搞懂yolo
    XXE XML外部实体注入
    VPP二层接口,不是翻墙
  • 原文地址:https://blog.csdn.net/qq_63524487/article/details/132940348