spring mvc环境之异常的处理(十四)

 

 springMVC要处理异常需实现顶级HandlerExceptionResolver 接口;spring提供了很多的实现类,

其中ExecptionHandlerExceptionResolver实现类,主要提供了@ExceptionHandler注解处理异常(把控制器的异常通过注解,导向被注解标注的方法里).

其中ResponseStatusExceptionResolver实现类,主要提供了@ResponseStatus注解标注类或方法。(重新定义异常的错误码和错误信息,新建类继承Exception或Error接口).

其中DefaultHandlerExceptionResolver实现类,常见的异常加了一些扩展.

SimpleMappingExceptionResolver实现类,通过配置xml实现一些异常.

 =============================

目录

  • 实现 HandlerExceptionResolver 接口
  • 使用 @ExceptionHandler 注解
  • 使用 @ControllerAdvice+ @ ExceptionHandler 注解
  • 使用 @ResponseStatus

 ======================

 1.自定义异常实现HandlerExceptionResolver 接口

@Component
public class GlobalExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        System.out.println(e.getMessage());
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg",e.getMessage());
        mv.setViewName("error");
        return mv;
    }
}

 

然后在spring配置文件中配置刚才新建的类,或者加上@Component注解。

<!--全局异常捕捉 -->
<bean class="com.xxx.exception.GlobalExceptionResolver" />

 

 2. @ExceptionHandler能对当前Controller中指定的异常进行处理,可以通过ModelAndView将异常信息传递给页面。

使用如下:

@GetMapping("e1")
public String exception() {
    int i = 1 / 0;
    return "exception";
}

@ExceptionHandler(value = RuntimeException.class)
public ModelAndView handlerRuntimeException(Exception exception) {
    ModelAndView mv = new ModelAndView("error");
    mv.addObject("exception", exception);
    return mv;
}

@ExceptionHandler要注意异常的优先级,如果Controller中有如下的异常处理方法,那么还是会执行handlerArithmeticException()方法,因为ArithmeticException异常更加具体.

 

3.@ControllerAdvice处理全局异常 , @ExceptionHandler的作用域是当前Controller;

如果想对全局的异常进行处理需要使用@ControllerAdvice +  @ExceptionHandler。

使用如下:

@ControllerAdvice
public class ExceptionHandlerControllerAdvice {

    @ExceptionHandler(value = ArithmeticException.class)
    public ModelAndView handlerArithmeticException(Exception exception) {
        ModelAndView mv = new ModelAndView("error");
        mv.addObject("exception", exception);
        return mv;
    }

}

 

4.@ResponseStatus定制错误码和错误内容

可以使用@ResponseStatus加到异常类上,这样当这个异常抛出时,页面显示的是定制的错误消息。

异常类定义如下:

@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "用户名和密码不匹配")
public class UsernameNotMatchPasswordException extends RuntimeException {
}

在方法中抛出这个异常:

/**
     * 使用@ResponseStatus定义错误码和错误内容
     * @param i
     * @return
     */
@GetMapping("e3")
public String exception3(int i) {
    if (13 == i) {
        throw new UsernameNotMatchPasswordException();
    }
    return "success";
}

页面显示的错误消息如下:

 

 

 

5. 使用SimpleMappingExceptionResolver处理指定异常

   也可以使用spring-mvc.xml配置的方式,指定异常后显示错误页面.

 

转:

https://blog.csdn.net/u022812849/article/details/124913422

https://blog.csdn.net/zidongxiangxi/article/details/124501450

https://www.cnblogs.com/fps2tao/p/13526773.html

 

posted @ 2022-12-13 17:12  与f  阅读(51)  评论(0)    收藏  举报