参考:https://blog.csdn.net/baichoufei90/article/details/82717850
参考:https://blog.csdn.net/butioy_org/article/details/78718405
参考:http://c.biancheng.net/view/4466.html
参考:https://www.cnblogs.com/demingblog/p/9218271.html
参考:https://www.cnblogs.com/lenve/p/10748453.html
SpringMVC处理异常主要是是通过实现HandlerExceptionResolver 接口来处理,根据SpringMVC提供的方法,实现如下:
首先,自定义异常类
public class MyException extends Exception {
public MyException(){
super();
}
public MyException(String message){
super(message);
}
}
1、使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver,该方法只需要配置SpringMVC的配置文件即可,定义各类异常与错误视图的映射关系。
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="my-error"></property>
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<prop key="exception.MyException">my-error</prop>
<prop key="java.sql.SQLException">sql-error</prop>
</props>
</property>
</bean>
2、实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器,该接口用于解析请求处理过程中所产生的异常,但是不能处理控制器方法调用之前的异常,比如表单绑定数据转换异常。
该方法需要定义HandlerExceptionResolver的实现类,并将其在配置文件中托管给SpringMVC框架才能进行异常的统一处理。
public class MyExceptionHandler implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler,Exception ex){
ModelAndView mv;
if(ex instanceof MyException){
mv=new ModelAndView("my-error");
} else if(ex instanceof SQLException){
mv=new ModelAndView("sql-error");
} else{
mv=new ModelAndView("error");
}
mv.addObject("ex",ex);
return mv;
}
}
<bean id="exceptionResolver" class="exception.MyExceptionHandler"></bean>
3、使用 @ExceptionHandler 注解实现异常处理,该注解用在controller上,不需要在配置文件中处理,当该控制器中的方法出现异常时,触发该注解方法处理异常,可以创建 BaseController 类,并让需要处理异常的controller继承该类。
@ExceptionHandler
public String handleException(HttpServletRequest request,Exception ex){
request.setAttribute("ex", ex);
if (ex instanceof SQLException) {
return "sql-error";
} else if (ex instanceof MyException) {
return "my-error";
} else {
return "404";
}
}
4、同时使用@ExceptionHandler和@ControllerAdvice控制器增强 实现全局注解,同样不需要配置xml文件,推荐。
import exception.MyException;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
//全局异常处理
@ControllerAdvice
public class MyGlobalExceptionHandler {
@ExceptionHandler
public String handleException(HttpServletRequest request, Exception ex) throws Exception{
if (AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class) != null) {
throw ex;
}
request.setAttribute("ex", ex);
if (ex instanceof SQLException) {
return "sql-error";
} else if (ex instanceof MyException) {
return "my-error";
} else {
return "404";
}
}
}
5、配置web.xml的error-page项处理统一处理错误,参考https://blog.csdn.net/baichoufei90/article/details/82717850
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/velocity/template/404.vm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/exception/404</location>
</error-page>
@RequestMapping("/error/exception/404")
public String return404Vm(HttpServletRequest request,Model model){
model.addAttribute("errorMsg", "404 not found,host:" + request.getRemoteAddr());
return "404";
}