-
Use @ ExceptionHandler comment
-
Achieve HandlerExceptionResolver Interface
-
Use @controlleradvice comment
1. Use @ ExceptionHandler comment
The notes have a place to use bad thing: the method of exception handling and methods must be in the same error Controller inside, you can see, the biggest drawback of this approach is not global control abnormal. Each class should write again.
@Controller public class the UserController { / ** * simulate a NullPointerException * @return * / @RequestMapping ( "/ show1") public String the showInfo () { String STR = null; str.length (); return "index"; } / * * * simulation of an ArithmeticException * @return * / @RequestMapping ( "/ Show2") public String showInfo2 () { int A = 10/0; return "index"; } / ** * java.lang.ArithmeticException * this method needs to return a ModelAndView: the purpose is to allow us to specify the package exception information and views * Parameter Exception e: abnormal objects will be injected into the process * / @ExceptionHandler (java.lang.ArithmeticException.class value = {}) public ModelAndView arithmeticExceptionHandler (Exception E) { ModelAndView Music Videos new new ModelAndView = (); mv.addObject ( "error", e.toString ()); mv.setViewName ( "ERROR1"); return Music Videos; } / ** * java.lang.NullPointerException * this method needs to return a ModelAndView: object package allows us abnormality information and optionally FIG designated * parameter exception e: abnormal objects will be injected into the process * / @ExceptionHandler (java.lang.NullPointerException.class value = {}) public ModelAndView nullPointerExceptionHandler (exception E) { ModelAndView mv = new ModelAndView(); mv.addObject("error", e.toString()); mv.setViewName("error2"); return mv; } }
2. Use @controlleradvice comment + @ ExceptionHandler
@ ExceptionHandler method described above comes the need for exception handling must be an error in the same method Controller inside. So when the code added @ControllerAdvice, you do not need to be in the same controller. It also brings new features 3.2 Spring. As can be seen from the name of the controller means substantially enhanced. In other words, @ controlleradvice + @ ExceptionHandler can achieve global exception catch.
Make sure that this class WebExceptionHandle to be scanned and loaded into vessel Spring
@ControllerAdvice public class GlobalException { / ** * java.lang.ArithmeticException * This method needs to return a ModelAndView: object package allows us to view the information specified exception * Parameter Exception e: abnormal objects will be injected into the process * / @ExceptionHandler (java.lang.ArithmeticException.class value = {}) public ModelAndView arithmeticExceptionHandler (Exception E) { ModelAndView Music Videos new new ModelAndView = (); mv.addObject ( "error", e.toString () + "- the advice "); mv.setViewName (" ERROR1 "); return Music Videos; } / ** * java.lang.NullPointerException * this method needs to return a ModelAndView: object package allows us abnormality information and optionally FIG designated * Parameter Exception e: abnormal objects will be injected into the process * / @ExceptionHandler (java.lang.NullPointerException.class value = {}) public ModelAndView nullPointerExceptionHandler (Exception E) { ModelAndView Music Videos new new ModelAndView = (); mv.addObject ( "error", e.toString () + "- the advice"); mv.setViewName ( "Error2"); return Music Videos; } }
If @ExceptionHandler annotation does not state the type of exception to be processed, then the default exception type parameter list . It can also be written like this:
@ControllerAdvice
public class GlobalExceptionHandler { @ExceptionHandler() @ResponseBody String handleException(Exception e){ return "Exception Deal! " + e.getMessage(); } }
The controller only business processing code
@Controller public class UserController { /** * 模拟 NullPointerException * @return */ @RequestMapping("/show1") public String showInfo(){ String str = null; str.length(); return "index"; } /** * 模拟 ArithmeticException * @return */ @RequestMapping("/show2") public String showInfo2(){ int a = 10/0; return "index"; } }
3. achieve HandlerExceptionResolver Interface
Project exception requires a unified process, under normal circumstances, need to be ready ahead of an error page when the project is wrong, the page displayed to the user.
@Component //注意该类需要交给Spring容器管理
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object obj,Exception ex) {
System.out.println(ex.getMessage());
ModelAndView mv = new ModelAndView();
mv.setViewName("/error.jsp");
return mv;
}
}