前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC统一异常处理

SpringMVC统一异常处理

作者头像
Tim在路上
发布2020-08-04 22:05:53
3760
发布2020-08-04 22:05:53
举报

Spring MVC处理异常有3种方式:

(1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver;

(2)实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器;

(3)使用@ExceptionHandler注解实现异常处理;

但是方式一仅能获取到异常信息,方式三对已有代码存在入侵性。

实现HandlerExceptionResolver 接口异常处理器

1、增加HandlerExceptionResolver 接口的实现类MyExceptionHandler,代码如下:

代码语言:javascript
复制
public class MyExceptionHandler implements HandlerExceptionResolver {  
  
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  
            Exception ex) {  
        Map<String, Object> model = new HashMap<String, Object>();  
        model.put("ex", ex);  
          
        // 根据不同错误转向不同页面  
        if(ex instanceof BusinessException) {
            return new ModelAndView("error-business", model);  
        }else if(ex instanceof ParameterException) {  
            return new ModelAndView("error-parameter", model);  
        } else {  
            return new ModelAndView("error", model);  
        }  
    }  
}  
  1. 将Bean添加到Bean工厂,可以在Spring的配置文件applicationContext.xml中增加以下内容
代码语言:javascript
复制
<bean id="exceptionHandler" class="cn.basttg.core.exception.MyExceptionHandler"/> 

或可以使用注解以及扫描添加。

实现定义的异常类即可:

代码语言:javascript
复制
public class BusinessException extends RuntimeException {    
    
    /** serialVersionUID */    
    private static final long serialVersionUID = 2332608236621015980L;    
    
    private String code;    
    
    public BusinessException() {    
        super();    
    }    
    
    public BusinessException(String message) {    
        super(message);    
    }    
    
    public BusinessException(String code, String message) {    
        super(message);    
        this.code = code;    
    }    
    
    public BusinessException(Throwable cause) {    
        super(cause);    
    }    
    
    public BusinessException(String message, Throwable cause) {    
        super(message, cause);    
    }    
    
    public BusinessException(String code, String message, Throwable cause) {    
        super(message, cause);    
        this.code = code;    
    }    
    
    public String getCode() {    
        return code;    
    }    
    
    public void setCode(String code) {    
        this.code = code;    
    }    
    
}    
    
    
public class ParameterException extends RuntimeException {    
    
    /** serialVersionUID */    
    private static final long serialVersionUID = 6417641452178955756L;    
    
    public ParameterException() {    
        super();    
    }    
    
    public ParameterException(String message) {    
        super(message);    
    }    
    
    public ParameterException(Throwable cause) {    
        super(cause);    
    }    
    
    public ParameterException(String message, Throwable cause) {    
        super(message, cause);    
    }    
}  

如果我们只相对控制器添加统一的异常捕获程序,最简单的方法就是使用@ControllerAdvice 注解

为控制器添加通知

@ControllerAdvice已经包含了@Component,所以它会自动被组件扫描获取到

代码语言:javascript
复制
@ControllerAdvice
public  class  AppWideExceptionHandler{
    
    @ExceptionHandler(CommonException.class)
    public String commonHandler(){
        return "error/commonError";
    }
}

重定向异常就是

代码语言:javascript
复制
return "redirect:/error"
``
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现HandlerExceptionResolver 接口异常处理器
  • 为控制器添加通知
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档