六、SpringMVC的异常处理
6.1 SpringMVC异常概述
我们在处理异常时,通常使用try…catch块来处理程序中发生的异常;出现异常时每个过程都单独处理异常,系统的代码耦合度高,工作量大且不好统一,维护的工作量也很大。
SpringMVC提供的异常处理机制可以把异常处理抽离成方法、类;让处理异常的代码与我们的业务代码完全分开,降低与业务核心代码的耦合度;
6.2 SpringMVC异常处理
6.2.1 单个类处理方式
- Controller:
package com.dfbz.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** @author lscl* @version 1.0* @intro:*/
@Controller
@RequestMapping("/demo01")
public class Demo01Controller {@RequestMapping("/demo01")@ResponseBodypublic String demo01(Integer flag) {if (flag == 0) {// 模拟异常int i = 1 / 0;}return "success";}/*** 如果出现了ArithmeticException就进入此方法** @param model: BingMap对象* @param e: 触发的异常对象* @return*/@ExceptionHandler(value = {ArithmeticException.class})public String arithmeticExceptionHandler(Model model, Exception e) {model.addAttribute("message",e.getMessage());return "forward:/error.jsp";}/*** 出现异常就进入此方法(如果能匹配具体子类则进入子类异常)* @param model* @param e* @return*/@ExceptionHandler(value = {Exception.class})public String exceptionHandler(Model model, Exception e) {model.addAttribute("message", e.getMessage());return "forward:/error.jsp";}
}
6.2.2 处理全局异常
我们可以编写一个类实现实现HandlerExceptionResolver接口来处理全局异常
package com.dfbz.exception;import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;/*** @author lscl* @version 1.0* @intro: 自定义异常处理类*/
@Component
public class MyExceptionResolver implements HandlerExceptionResolver {@Overridepublic ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {System.out.println(handler.getClass());System.out.println(ex.getClass());ModelAndView mv = new ModelAndView();mv.addObject("message", ex.getMessage());mv.setViewName("/error.jsp");return mv;}
}
Tips:如果异常被Controller中的某个ExceptionHandler方法匹配了,那么不会进入自定义的异常解析器;
6.2.3 注解方式实现全局异常
-
@ControllerAdvice
:注解可以配置全局异常 -
@ExceptionHandler(Exception.class)
:设置什么异常才会进入方法
package com.dfbz.exception;import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;/*** @author lscl* @version 1.0* @intro: 使用注解方式配置全局异常*/
@ControllerAdvice
public class AnnoMyExceptionHandler {/*** 出现ArithmeticException时进入此方法* @param model* @param e* @return*/@ExceptionHandler(value = ArithmeticException.class) public String arithmeticExceptionHandler(Model model, Exception e) {model.addAttribute("message", e.getMessage());return "forward:/error.jsp";}/*** 出现Exception时进入此方法* @param e* @return*/@ExceptionHandler(Exception.class)public String exceptionHandler(Model model, Exception e) {model.addAttribute("message", e.getMessage());return "forward:/error.jsp";}
}
Tips:
- 1)当同时定义了异常解析器(HandlerExceptionResolver)和@ControllerAdvice时,@ControllerAdvice优先级高;
- 2)如果@ControllerAdvice标注的异常和Controller方法定义的异常冲突,则以Controller中标注有@ExceptionHandler的方法优先;