SpringMVC 异常集中处理

当我们在实际开发中,遇到有可能会抛出异常的代码我们往往用try  - catch代码块去将代码包裹起来。

或者将异常抛出去。

这两种方式我们来分析一下。

用try-catch捕获异常的方式是可行的。

但是个人觉得try-catch代码有点丑陋。

直接将异常抛出的方式也行,但是没有对异常做出处理。异常还是会在调用这个方法的地方提示需要捕获异常。

所以我们最终的解决办法还是要捕获异常。并作出处理。

但是每次总是分开来处理总觉得不是太好,所以这里笔者将要介绍SpringMVC异常集中处理的方式(SpringMVC提供了3种方式)。

1)

首先我们写一个controller

package org.huluo.exceptionintegratedhandle.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExceptionIntegratedHandleController {

    @RequestMapping("/exceptionIntegratedHandleController/{id}")
    @ResponseBody
    public Object hello(@PathVariable String id) {

        if (id.equals("1")) {
            throw new RuntimeException("我这里是手动抛出的异常,期望被SpringMVC集中处理");
        } else if (id.equals("2")) {
            int value = 1 / 0;
            return "手动运算错误";
        } else {
            return "没有手动抛异常哦";
        }
    }
}

这里我们根据id的值来手动的抛出异常,

第一种解决方式是配置文件的方式

第一种解决方式
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        默认的错误信息页面
        <property name="defaultErrorView" value="error"/>
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.RuntimeException">runtimeExceptionPage</prop>
                <prop key="java.lang.ArithmeticException">arithmeticExceptionPage</prop>
            </props>
        </property>
    </bean>

配置文件可以根据抛出的异常类的类信息配置错误的页面。

但是这样我们无法手动对其定制

故.这里有第二种解决方式

     第二种方式是自己编码的方式,定制异常集中处理
    <bean id="mySpringExceptionIntegratedHandler"
          class="org.huluo.exceptionintegratedhandle.MySpringExceptionIntegratedHandler"/>

自己编写类去 实现HandlerExceptionResolver接口

package org.huluo.exceptionintegratedhandle;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

//自己手动编写Java代码来实现定制异常信息处理
public class MySpringExceptionIntegratedHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, Exception ex) {
        //这里可以根据异常的类型来决定什么样的对策
        System.out.println("异常已经被处理了");
        System.out.println("异常的类型是:" + ex.getClass().getName());
        return new ModelAndView("runtimeExceptionPage");
    }
}


最后还有第三种方式去实现异常的集中处理


就是基于继承和注解

这里笔者写一个Controller 

@Controller
public class ExceptionIntegratedHandleController1 extends ExceptionIntegratedHandleBaseController {

    @RequestMapping("/world")
    @ResponseBody
    public Object world() {
        throw new RuntimeException("我这里是手动抛出的异常,期望被SpringMVC集中处理");
    }

}


ExceptionIntegratedHandleBaseController这个类是自己定义的,里面的实现是

package org.huluo.exceptionintegratedhandle.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

//以注解加继承的方式实现Spring的异常统一处理
//期望被统一处理的controller需要继承这个类
public class ExceptionIntegratedHandleBaseController {

    @ExceptionHandler
    public String handleException(HttpServletRequest request, Exception ex) {
        System.out.println("异常被处理掉了:以注解加继承的方式");
        System.out.println("异常的信息是:" + ex.getClass().getName());
        return "error";
    }
}


期望异常被集中处理的controller应该继承这个自己手写的类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值