參考: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";
}