Spring MVC统一异常处理的3种方式
在 Spring MVC 应用的开发中,不管是对底层数据库操作,还是业务层或控制层操作,都会不可避免地遇到各种可预知的、不可预知的异常需要处理。
如果每个过程都单独处理异常,那么系统的代码耦合度高,工作量大且不好统一,以后维护的工作量也很大。
如果能将所有类型的异常处理从各层中解耦出来,这样既保证了相关处理过程的功能单一,又实现了异常信息的统一处理和维护。
幸运的是,Spring MVC 框架支持这样的实现。Spring MVC 统一异常处理有以下 3 种方式:
- 使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver。
- 实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器。
- 使用 @ExceptionHandler 注解实现异常处理
示例
1)创建工程并导入JAR包
2)创建自定义异常类
package pers.zhang.exception;
public class MyException extends Exception {
private static final long serialVersionUID = 1L;
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
3)创建 Dao 层
创建 TestExceptionDao 类,在该类中定义 3 个方法,分别抛出“数据库异常”“自定义异常”和“未知异常”。具体代码如下:
package pers.zhang.dao;
import java.sql.SQLException;
import org.springframework.stereotype.Repository;
@Repository("TestExceptionDao")
public class TestExceptionDao {
public void daodb() throws Exception {
throw new SQLException("Dao中数据库异常");
}
public void daomy() throws Exception {
throw new SQLException("Dao中自定义异常");
}
public void daono() throws Exception {
throw new SQLException("Dao中未知异常");
}
}
4)创建 Service 层
创建 TestExceptionService 接口和 TestExceptionServiceImpl 实现类,在该接口中定义 6 个方法,其中有 3 个方法调用 Dao 层中的方法,有 3 个是 Service 层的方法。
Service 层的方法是为演示 Service 层的“数据库异常”“自定义异常”和“未知异常”而定义的。
TestExceptionService 接口的代码如下:
package pers.zhang.service;
public interface TestExceptionService {
public void servicemy() throws Exception;
public void servicedb() throws Exception;
public void daomy() throws Exception;
public void daodb() throws Exception;
public void serviceno() throws Exception;
public void daono() throws Exception;
}
package pers.zhang.service;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.zhang.dao.TestExceptionDao;
import pers.zhang.exception.MyException;
@Service("testExceptionService")
public class TestExceptionServiceImpl implements TestExceptionService {
@Autowired
private TestExceptionDao testExceptionDao;
@Override
public void servicemy() throws Exception {
throw new MyException("Service中自定义异常");
}
@Override
public void servicedb() throws Exception {
throw new SQLException("Service中数据库异常");
}
@Override
public void daomy() throws Exception {
testExceptionDao.daomy();
}
@Override
public void daodb() throws Exception {
testExceptionDao.daodb();
}
@Override
public void serviceno() throws Exception {
throw new SQLException("Service中未知异常");
}
@Override
public void daono() throws Exception {
testExceptionDao.daono();
}
}
5)创建控制器类
package pers.zhang.controller;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pers.zhang.exception.MyException;
import pers.zhang.service.TestExceptionService;
@Controller
public class TestExceptionController {
@Autowired
private TestExceptionService testExceptionService;
@RequestMapping("/db")
public void db() throws Exception {
throw new SQLException("控制器中数据库异常");
}
@RequestMapping("/my")
public void my() throws Exception {
throw new MyException("控制器中自定义异常");
}
@RequestMapping("/no")
public void no() throws Exception {
throw new Exception("控制器中未知异常");
}
@RequestMapping("/servicedb")
public void servicedb() throws Exception {
testExceptionService.servicedb();
}
@RequestMapping("/servicemy")
public void servicemy() throws Exception {
testExceptionService.servicemy();
}
@RequestMapping("/serviceno")
public void serviceno() throws Exception {
testExceptionService.serviceno();
}
@RequestMapping("/daodb")
public void daodb() throws Exception {
testExceptionService.daodb();
}
@RequestMapping("/daomy")
public void daomy() throws Exception {
testExceptionService.daomy();
}
@RequestMapping("/daono")
public void daono() throws Exception {
testExceptionService.daono();
}
}
6)创建 View 层
测试应用首页面 index.jsp 的代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>
<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"%>
<%@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>
资源已不在。
</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>
7)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCErrorDemo</display-name>
<!--避免中文乱码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/404.jsp</location>
</error-page>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认找 /WEB-INF/[servlet的名称]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第一种方式:使用SimpleMappingExceptionResolver类异常处理
使用 org.springframework.web.servlet.handler.SimpleMappingExceptionResolver 类统一处理异常时需要在配置文件中提前配置异常类和 View 的对应关系。配置文件 springmvc-servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring一beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用扫描机制扫描包 -->
<context:component-scan base-package="controller" />
<context:component-scan base-package="service" />
<context:component-scan base-package="dao" />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!--前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!--SimpleMappingExceptionResolver(异常类与 View 的对应关系) -->
<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>
第二种方式:使用HandlerExceptionResolver接口异常处理
org.springframework.web.servlet.HandlerExceptionResolver 接口用于解析请求处理过程中所产生的异常。开发者可以开发该接口的实现类进行 Spring MVC 应用的异常统一处理。
在 springMVCDemo10 应用的 exception 包中创建一个 HandlerExceptionResolver 接口的实现类 MyExceptionHandler,具体代码如下:
package pers.zhang.exception;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class MyExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("ex", arg3);
// 根据不同错误转向不同页面(统一处理),即异常与View的对应关系
if (arg3 instanceof MyException) {
return new ModelAndView("my-error", model);
} else if (arg3 instanceof SQLException) {
return new ModelAndView("sql-error", model);
} else {
return new ModelAndView("error", model);
}
}
}
在实现 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@Controler @Service -->
<context:component-scan base-package="pers.zhang"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**"/>
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--托管MyExceptionHandler-->
<bean class="pers.zhang.exception.MyExceptionHandler"/>
</beans>
第三种方式:使用@ExceptionHandler注解异常处理
创建 BaseController 类,并在该类中使用 @ExceptionHandler 注解声明异常处理方法,具体代码如下:
package pers.zhang.controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import pers.zhang.exception.MyException;
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 TestExceptionController 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@Controler @Service -->
<context:component-scan base-package="pers.zhang"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**"/>
<!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--托管MyExceptionHandler-->
<!-- <bean class="pers.zhang.exception.MyExceptionHandler"/> -->
</beans>
测试