第十二章 异常处理

target

理解使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver处理异常
理解实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器处理异常
理解使用 @ExceptionHandler 注解实现异常处理

在 Spring MVC应用的开发中,不管是对底层数据库操作,还是业务层或控制层操作,都会不可避免地遇到各种可预知的、不可预知的异常需要处理。

如果每个过程都单独处理异常,那么系统的代码耦合度高,工作量大且不好统一,以后维护的工作量也很大。

如果能将所有类型的异常处理从各层中解耦出来,这样既保证了相关处理过程的功能单一,又实现了异常信息的统一处理和维护。幸运的是,Spring MVC 框架支持这样的实现。Spring MVC 统一异常处理有以下 3 种方式:

  • 使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver。
  • 实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器。
  • 使用 @ExceptionHandler 注解实现异常处理

为了验证 Spring MVC 框架的 3 种异常处理方式的实际效果,需要开发一个测试应用 springMVCDemo,从 Dao 层、Service 层、Controller 层分别抛出不同的异常(SQLException、自定义异常和未知异常),然后分别集成 3 种方式进行异常处理,进而比较其优缺点。springMVCException 应用的结构如图所示。

3 种异常处理方式的相似部分有 Dao 层、Service 层、View 层、MyException、TestException Controller 以及 web.xml,下面分别介绍这些相似部分。

1. 项目构建

1.1 创建自定义异常类

在 src 目录下创建 exception 包,并在该包中创建自定义异常类 MyException。具体代码如下:

public class MyException extends Exception {

    private static final long serialVersionUID = 1L;

    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }
}

1.2 创建 Dao 层

在 src 目录下创建 dao 包,并在该包中创建 TestExceptionDao 类,在该类中定义 3 个方法,分别抛出"数据库异常"、"自定义异常"和"未知异常"。具体代码如下:

@Repository("exceptionDao")
public class ExceptionDao {

    public void daodb() throws SQLException {
        throw new SQLException("Dao中数据库异常");
    }

    public void daomy() throws MyException {
        throw new MyException("Dao中自定义异常");
    }

    public void daono() throws Exception {
        throw new Exception("Dao中未知异常");
    }
}

1.3 创建 Service 层

在 src 目录下创建 service 包,并在该包中创建 ExceptionService 接口和 ExceptionServiceImpl 实现类,在该接口中定义 6 个方法,其中有 3 个方法调用 Dao 层中的方法,有 3 个是 Service 层的方法。

Service 层的方法是为演示 Service 层的"数据库异常"、"自定义异常"和"未知异常"而定义的。

ExceptionService 接口的代码如下:

public interface ExceptionService {

  public void servicemy() throws MyException;//service的自定义异常

  public void servicedb() throws SQLException;//service数据库异常

  public void serviceno() throws Exception;//service未知异常

  public void daomy() throws MyException;//dao的自定义异常

  public void daodb() throws SQLException;//dao的数据库异常

  public void daono() throws Exception;//dao的未知异常

}

ExceptionServiceImpl 实现类的代码如下:

@Service("exceptionService")
public class ExceptionServiceImpl implements ExceptionService {

    @Autowired
    private ExceptionDao exceptionDao;
    
    @Override
    public void servicemy() throws MyException {
        throw new MyException("Service中自定义异常");
    }
    @Override
    public void servicedb() throws SQLException {
        throw new SQLException("Service中数据库异常");
    }

    @Override
    public void serviceno() throws Exception {
        throw new SQLException("Service中未知异常");
    }
    @Override
    public void daomy() throws MyException {
        exceptionDao.daomy();
    }
    @Override
    public void daodb() throws SQLException {
        exceptionDao.daodb();
    }
  
    @Override
    public void daono() throws Exception {
        exceptionDao.daono();
    }
}

1.4 创建控制器类

在 src 目录下创建 controller 包,并在该包中创建 ExceptionController 控制器类,代码如下:

@Controller
public class ExceptionController {

    @Autowired
    private ExceptionService exceptionService;

    @RequestMapping("/db")
    public void db() throws SQLException{
        throw new SQLException("控制器中数据库异常");
    }

    @RequestMapping("/my")
    public void my() throws MyException{
        throw new MyException("控制器中自定义异常");
    }

    @RequestMapping("/no")
    public void no() throws Exception {
        throw new Exception("控制器中未知异常");
    }

    @RequestMapping("/servicedb")
    public void servicedb() throws SQLException {
        exceptionService.servicedb();
    }

    @RequestMapping("/servicemy")
    public void servicemy() throws MyException {
        exceptionService.servicemy();
    }

    @RequestMapping("/serviceno")
    public void serviceno() throws Exception {
        exceptionService.serviceno();
    }

    @RequestMapping("/daodb")
    public void daodb() throws SQLException {
        exceptionService.daodb();
    }

    @RequestMapping("/daomy")
    public void daomy() throws MyException {
        exceptionService.daomy();
    }

    @RequestMapping("/daono")
    public void daono() throws Exception {
        exceptionService.daono();
    }
}

1.5 创建 View 层

View 层中共有 5 个 JSP 页面,下面分别介绍。

测试应用首页面 index.jsp 的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
    <h1>所有的演示例子</h1>
    <h3><a href="${pageContext.request.contextPath }/daodb"> 处理dao中数据库异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/daomy"> 处理dao中自定义异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/daono"> 处理dao未知错误 </a></h3>
    <hr>
    <h3><a href="${pageContext.request.contextPath }/servicedb">处理 service中数据库异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/servicemy">处理 service中自定义异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/serviceno">处理 service未知错误</a></h3>
    <hr>
    <h3><a href="${pageContext.request.contextPath }/db">处理 controller中数据库异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/my">处理 controller中自定义异常</a></h3>
    <h3><a href="${pageContext.request.contextPath }/no">处理 controller未知错误</a></h3>
    <hr>
    <!-- 在 web.xml中配置404 -->
    <h3>
        <a href="${pageContext.request.contextPath }/404">404 错误</a>
    </h3>
</body>
</html>

404 错误对应页面 404.jsp 的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
资源不存在!
</body>
</html>

未知异常对应页面 error.jsp 的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <H1>未知错误:</H1><%=exception %>
    <H2>错误内容:</H2>
    <%
        exception.printStackTrace(response.getWriter());
    %>
</body>
</html>

自定义异常对应页面 my-error.jsp 的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <H1>自定义异常错误:</H1><%=exception %>
    <H2>错误内容:</H2>
    <%
        exception.printStackTrace(response.getWriter());
    %>
</body>
</html>

SQL 异常对应页面 sql-error.jsp 的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <H1>数据库异常错误:</H1><%=exception %>
    <H2>错误内容:</H2>
    <%
        exception.printStackTrace(response.getWriter());
    %>
</body>
</html>

1.6 web.xml

对于 Unchecked Exception 而言,由于代码不强制捕获,往往被忽略,如果运行期产生了 Unchecked Exception,而代码中又没有进行相应的捕获和处理,则可能不得不面对 404、500 等服务器内部错误提示页面,所以在 web.xml 文件中添加了全局异常 404 处理。具体代码如下:

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/views/404.jsp</location>
</error-page>

从上述 Dao 层、Service 层以及 Controller 层的代码中可以看出,它们只管通过 throw 和 throws 语句抛出异常,并不处理。下面分别从 3 种方式统一处理这些异常。

2. 使用SimpleMappingExceptionResolver类异常处理

使用 org.springframework.web.servlet.handler.SimpleMappingExceptionResolver 类统一处理异常时需要在配置文件中提前配置异常类和 View 的对应关系。配置文件 springmvc.xml 的具体代码如下:

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 配置扫描 -->
    <context:component-scan base-package="com.lee"></context:component-scan>

    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <!-- 定义默认的异常处理页面,当该异常类型注册时使用 -->
        <property name="defaultErrorView" value="error"></property>
        <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
        <property name="exceptionAttribute" value="ex"></property>
        <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 -->
        <property name="exceptionMappings">
            <props>
                <prop key="exception.MyException">my-error</prop>
                <prop key="java.sql.SQLException">sql-error</prop>
                <!-- 在这里还可以继续扩展对不同异常类型的处理 -->
            </props>
        </property>
    </bean>

</beans>

在配置完成后就可以通过 SimpleMappingExceptionResolver 异常处理器统一处理上面的异常。

发布 springMVCException应用到 Tomcat 服务器并启动服务器,然后即可通过地址"http://localhost:8080/springMVCException/"测试应用。

3. 使用HandlerExceptionResolver接口异常处理

org.springframework.web.servlet.HandlerExceptionResolver 接口用于解析请求处理过程中所产生的异常。开发者可以开发该接口的实现类进行 Spring MVC应用的异常统一处理。

在 springMVCException 应用的 exception 包中创建一个 HandlerExceptionResolver 接口的实现类 MyExceptionHandler,具体代码如下:

public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception exception) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("ex", exception);
        // 根据不同错误转向不同页面(统一处理),即异常与View的对应关系
        if (exception instanceof MyException) {
            return new ModelAndView("my-error", model);
        } else if (exception instanceof SQLException) {
            return new ModelAndView("sql-error", model);
        } else {
            return new ModelAndView("error", model);
        }
    }
}

需要将实现类 MyExceptionHandler 在配置文件中托管给 Spring MVC 框架才能进行异常的统一处理,配置代码为<bean class="exception.MyExceptionHandler"/>。

在实现 HandlerExceptionResolver 接口统一处理异常时将配置文件的代码修改如下:

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 配置扫描 -->
    <context:component-scan base-package="com.lee"></context:component-scan>

    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--托管MyExceptionHandler-->
    <bean class="exception.MyExceptionHandler"/>
</beans>

4. 使用@ExceptionHandler注解异常处理

创建 BaseController 类,并在该类中使用 @ExceptionHandler 注解声明异常处理方法,具体代码如下:

public class BaseController {
    /** 基于@ExceptionHandler异常处理 */
    @ExceptionHandler
    public String exception(HttpServletRequest request, Exception ex) {
        request.setAttribute("ex", ex);
        // 根据不同错误转向不同页面,即异常与view的对应关系
        if (ex instanceof SQLException) {
            return "sql-error";
        } else if (ex instanceof MyException) {
            return "my-error";
        } else {
            return "error";
        }
    }
}

将所有需要异常处理的 Controller 都继承 BaseController 类,示例代码如下:

@Controller
public class ExceptionController extends BaseController{
 ...
}

在使用 @ExceptionHandler 注解声明统一处理异常时不需要配置任何信息,此时将配置文件的代码修改如下:

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 配置扫描 -->
    <context:component-scan base-package="com.lee"></context:component-scan>

    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容