SSM之全局异常处理器

1. 异常处理思路

  首先来看一下在springmvc中,异常处理的思路: 
springmvc异常处理 
  如上图所示,系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。明白了springmvc中的异常处理机制,下面就开始分析springmvc中的异常处理。

 

2. 定义全局异常处理器

    1、定义自定义异常类,继承Exception

public class CustomException extends Exception {

    public String message;
    public Throwable throwable;
    public CustomException(String message){
        super(message);
        this.message = message;
    }

    public CustomException(Throwable throwable){
        super(throwable);
        this.throwable = throwable;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

2、定义异常处理器

public class CustomExceptionResolver implements HandlerExceptionResolver {


    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        // 解析出异常类型
        CustomException customException = null;
        String message = "";
        // 若该异常类型是系统自定义的异常,直接取出异常信息在错误页面展示即可。
        if(e instanceof CustomException){
            customException = (CustomException)e;
            customException.getThrowable().getClass().getName();
        }else{
            // 如果不是系统自定义异常,构造一个系统自定义的异常类型,信息为“未知错误”
            customException = new CustomException("未知错误");
            message = customException.getMessage();
        }
        //错误信息
        ModelAndView modelAndView = new ModelAndView();
        //将错误信息传到页面
        modelAndView.addObject("message",message);
        //指向错误页面
        modelAndView.setViewName("showError");
        return modelAndView;
    }
}

3、SpringMVC中配置全局异常处理器

 <bean class="com.smart.exception.CustomExceptionResolver"/>

4、测试

@RequestMapping(value="")
    @ResponseBody
    public Map<String,Object> Register(User user) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        try{
          boolean isSuccess = userService.Register(user);
          if(isSuccess){
             map.put("tip", "success");
          }
          else{
               map.put("tip", "error");
          }
        }catch (Exception e){
            throw new CustomException("未知错误");
        }
        return map;
    }

 

转载于:https://www.cnblogs.com/lwx521/p/8831695.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架的全局异常处理一般可以通过以下步骤实现: 1. 在Spring配置文件中配置异常解析器(HandlerExceptionResolver),以捕获所有Controller中抛出的异常。 2. 实现一个自定义异常类,并继承Exception类,用于在程序中抛出自定义异常。 3. 在Controller中抛出自定义异常,并在自定义异常类中添加构造函数,以方便传递异常信息。 4. 在异常解析器中处理异常,根据异常类型返回不同的错误信息。 下面是一个简单的实现示例: 1. 在Spring配置文件中配置异常解析器: ``` <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">error</prop> </props> </property> <property name="defaultErrorView" value="error"/> </bean> ``` 2. 自定义异常类: ``` public class MyException extends Exception { public MyException(String message) { super(message); } } ``` 3. 在Controller中抛出自定义异常: ``` @RequestMapping("/test") public String test() throws MyException { throw new MyException("test exception"); } ``` 4. 在异常解析器中处理异常: ``` @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView = new ModelAndView(); if (ex instanceof MyException) { modelAndView.addObject("message", ex.getMessage()); modelAndView.setViewName("error"); } else { modelAndView.addObject("message", "unknown error"); modelAndView.setViewName("error"); } return modelAndView; } ``` 在上面的示例中,如果Controller中抛出MyException异常,则异常解析器会返回错误信息"test exception",否则返回"unknown error"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值