spring框架做全局异常捕获_Spring 框架——利用HandlerExceptionResolver实现全局异常捕获...

微信

一、需求描述

因为在项目中,我们不可否认的会出现异常,而且这些异常并没有进行捕获。经常出现的bug如空指针异常等等。在之前的项目中,如果我们没有进行任何配置,那么容器会自动打印错误的信息,如果tomcat的404页面,400页面等等。如果我们在web.xml中进行如下配置,就会拦截错误,然后跳转到指定的错误页面。

500

/500.jsp

但是这已经落后了,现在我们通过实现spring的HandlerExceptionResolver接口来捕获所有的异常。

二、基本应用

首先我们新建GlobalExceptionResolver如下:

public class GlobalExceptionResolver implements HandlerExceptionResolver{

@Override

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,

Exception exception) {

//--------------------------------------------

return null;

}

}

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

现在就可以根据自己的需求修改GlobalExceptionResolver的横线部分了,在你在开发的时候,请返回null,这样这个类就如同不起作用,之前该怎么样就怎么样。当开发完成之后,根据错误的信息来返回需要的东西了。首先我们可以做的是打印错误日志,当然也是必须的。

System.err.println("访问" + request.getRequestURI() + " 发生错误, 错误信息:" + exception.getMessage());

这里我只是用控制台举例,你当然可以用日志框架打印。打印信息主要是包括是访问那个地址出现了什么错误。之后如果你需要返回错误页面,那么就直接在ModelAndView里面写就行了,这里就不多举例了,ModelAndView写过MVC的Controller应该都熟悉。

ModelAndView modelAndView = new ModelAndView();

modelAndView.setViewName("error");

return modelAndView;

以上其实就已经实现了全局捕获异常的功能,你可以自己抛出一个不捕获的异常测试一下是否成功。

三、其他说明

注意不同类型和来源的请求

因为在实际项目中,可能遇到各种请求类型,如正常的get,post。也可能是来自ajax的请求。所以如果均返回同一个ModelAndView显然可能有点不合适,对于ajax可能需要特别处理。

还有就是如果有手机端和PC在同一个项目中的情况,那么来源不同,返回的页面也可能不同。虽然可以交给前端去做自适应处理,但是我们还是得做好考虑。

总之,要考虑到各种不同的请求,单一返回可能并不适用所有项目。

GlobalExceptionResolver这个类推荐放在exception包下,属于一种自定义异常

这个配置推荐放在和web相关的spring配置下,因为和类似一个controller

spring是怎么做到的?handler参数又是什么?

首先spring官方文档536页说明了HandlerExceptionResolve,而官方推荐的是使用@ExceptionHandler注解去捕获固定的异常。然后我查了源码,spring源码中:

/**

* Resolve the exception by iterating over the list of configured exception resolvers.

* The first one to return a ModelAndView instance wins. Otherwise {@code null} is returned.

*/

@Override

public ModelAndView resolveException(HttpServletRequest request,

HttpServletResponse response,

Object handler,

Exception ex) {

if (resolvers != null) {

for (HandlerExceptionResolver handlerExceptionResolver : resolvers) {

ModelAndView mav = handlerExceptionResolver.resolveException(request, response, handler, ex);

if (mav != null) {

return mav;

}

}

}

return null;

}

这是spring默认实现,也就是说,我们没有重写的话,spring是这样执行的,从命名来瞎说就是。如果出现异常(private List resolvers;),处理异常解析器就会非空,通过循环异常解析器处理resolvers中的异常,然后处理。最后返回null也就是我们之前所说的不做任何错误页面的那种处理。然后处理异常打印异常信息是在抽象类里面完成的:

/**

* Check whether this resolver is supposed to apply (i.e. if the supplied handler

* matches any of the configured {@linkplain #setMappedHandlers handlers} or

* {@linkplain #setMappedHandlerClasses handler classes}), and then delegate

* to the {@link #doResolveException} template method.

*/

@Override

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,

Object handler, Exception ex) {

if (shouldApplyTo(request, handler)) {

if (this.logger.isDebugEnabled()) {

this.logger.debug("Resolving exception from handler [" + handler + "]: " + ex);

}

prepareResponse(ex, response);

ModelAndView result = doResolveException(request, response, handler, ex);

if (result != null) {

logException(ex, request);

}

return result;

}

else {

return null;

}

}

就是打印错误信息,这里我们看到handler被打印了。打印的意思是从哪一个handler解析出什么异常。最后如果有结果依旧返回。总之我们可以知道的是,spring的handle在处理时发现异常后,HandlerExceptionResolver的列表会被赋值,然后进行处理。

四、项目运用

这里我们可以参考《异常处理》一节阅读,首先我们的全局异常拦截器实现如下:

/**

*

* 类名称 : SgccExceptionResolver.

* 功能描述 : 全局异常拦截器,可在此做异常信息的判断及输出.

*

* 创建人: Administrator

* 创建时间 : 2017年7月3日 下午15:12:36.

*

* 修改历史:

* 修改人 修改日期   修改描述

* -----------------------------------

*/

public class SgccExceptionResolver implements HandlerExceptionResolver {

private Logger logger = Logger.getLogger(this.getClass());

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,

Exception ex) {

logger.info("==============Exception Start 000000=============");

if (ex instanceof BaseException) {

logger.debug(ex, ex);

}else {

logger.error(ex, ex);

}

logger.info("==============Exception End 000000=============");

if (NetworkUtil.isAjax(request)) {

String msg = null;

String code = null;

String detail = null;

if (ex instanceof BaseException) {

msg = ((BaseException) ex).getErrorMsg();

code = ((BaseException) ex).getErrorCode();

detail = ((BaseException) ex).getMsgDetail();

}else {

FSTErrorCode fc = FSTErrorCode.SYS_ERROR_000000;

msg = fc.getErrorMsg();

code = fc.getErrorCode();

detail = fc.getMsgDetail();

}

try {

Map map = new HashMap();

map.put("msg", msg);

map.put("code", code);

map.put("detail", detail);

JSONObject json = JSONObject.fromObject(map);

response.setContentType("text/html;charset=utf-8");

response.getWriter().print(json);

}catch (IOException e) {

e.printStackTrace();

}

return null;

}else {

ModelAndView mv = new ModelAndView();

mv.setViewName("error/error");

mv.addObject("exception", ex.toString().replaceAll("\n", "
"));

return mv;

}

}

}

__________________________________________________________________________________________________________

在开发中,异常很常见的,我们需要捕捉异常并处理它,才能保证程序不被终止。最常见的异常处理方法就是用try catch来捕捉异常。

Spring MVC通过HandlerExceptionResolver处理程序的异常,包括 Handler 映射、数据绑定以及目标方法执行时发生的异常。

SpringMVC 提供的 HandlerExceptionResolver 的实现类

DispatcherServlet 默认装配的 HandlerExceptionResolver :

如果添加了 则变为:

1、ExceptionHandlerExceptionResolver

ExceptionHandlerExceptionResolver主要是解析Handler中用 @ExceptionHandler注解定义的方法。

定义一个处理方法,处理过程中可能会存在抛出异常的情况:

@RequestMapping("testExceptionHandle")

public String testExceptionHandle(@RequestParam("i")Integer i){

System.out.println(10/i);

return "success";

}

当传递过来的 i = 0 时会产生数学异常

1.1 局部异常处理

在处理方法同一个类中定义异常处理方法:

//注意,该注解不是加在产生异常的方法上,而是加在处理异常的方法上

@ExceptionHandler({ArithmeticException.class})

public String testArithmeticException(Exception e){

System.out.println("打印错误信息 ===> ArithmeticException:"+e);

//跳转到指定页面

return "error";

}

当访问 http://localhost:8080/xxx/testExceptionHandle?i=0

时就会产生数学异常,被前置处理器捕获到,然后交给ExceptionHandlerExceptionResolver进行解析并分派处理,这里就交给了上面定义的异常处理方法testArithmeticException处理。

@ExceptionHandler 的value属性是一个Class 数组,因此我们可以在该注解的属性中加入多个异常类.

当目标方法产生异常时,将被这个方法捕捉,我们可以得到异常的实例这个异常处理只能处理当前类产生的异常,是局部的异常处理方法。

@ExceptionHandler注解定义的方法优先级问题:例如发生的是NullPointerException,但是声明的异常有RuntimeException和Exception,这时候会根据异常的最近继承关系找到继承深度最浅的那个@ExceptionHandler注解方法,即标记了RuntimeException 的方法

1.2 全局异常处理

新建一个处理异常的类,加上@ControllerAdvice注解

//标记当前类是用来处理异常的

@ControllerAdvice

public class HandleForException {

@ExceptionHandler({ArithmeticException.class})

public String testArithmeticException(Exception e){

System.out.println("打印错误信息 ===> ArithmeticException:"+e);

//跳转到指定页面

return "error";

}

}

当一个类加上@ControllerAdvice注解,告诉ExceptionHandlerExceptionResolver这个类是用来处理异常的。而具体的处理异常的方法加上@ExceptionHandler注解就行了。

ExceptionHandlerMethodResolver先在处理类内部找被@ExceptionHandler

注解的方法,如果找不到,就会去被@ControllerAdvice注解的的类中的 @ExceptionHandler 注解方法

注意:当被 @ExceptionHandler标记为异常处理方法,不能在方法中设置别的形参。但是可以使用ModelAndView向前台传递数据。

2、ResponseStatusExceptionResolver

自定义一个异常类,使用@ResponseStatus 注解标注。若在处理器方法中抛出了这个异常,由于触发的异常带有@ResponseStatus注解。因此会被ResponseStatusExceptionResolver解析到。最后响应HttpStatus.xx状态码和reason信息给客户端。关于其他的响应码请参考 HttpStatus 枚举类型源码。

具体的使用方法如下:

2.1 首先定义一个异常类:

@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户名和密码不匹配!")

public class UserNameNotMatchPasswordException extends RuntimeException{

}

2.2 人为抛出上述异常

@RequestMapping("/testResponseStatusExceptionResolver")

public String testResponseStatusExceptionResolver(@RequestParam("i") int i){

if (i==13){

throw new UserNameNotMatchPasswordException();

}

System.out.println("testResponseStatusExceptionResolver....");

return "success";

}

访问http://localhost:8080/xxx/testResponseStatusExceptionResolver?i=13

当然,也可以在方法上进行修饰:

@ResponseStatus(reason = "测试",value = HttpStatus.NOT_FOUND)

@RequestMapping("/testResponseStatusExceptionResolver")

public String testResponseStatusExceptionResolver(@RequestParam("i") int i){

if (i==13){

throw new UserNameNotMatchPasswordException();

}

System.out.println("testResponseStatusExceptionResolver....");

return "success";

}

这时所有的请求都会报错

不建议这么使用

3、DefaultHandlerExceptionResolver

对一些特殊的异常进行处理,比如NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException等。

会使用DefaultHandlerExceptionResolver进行解析,当发生这些异常时会返回给客户端一个处理过的页面,和ResponseStatusExceptionResolver很相似,只不过这里是系统为我们定义好的异常类和异常处理。

比如 NoSuchRequestHandlingMethodException异常:

定义一个处理方法,处理的是post请求:

@RequestMapping(value="/handleNoSuchRequestHandlingMethod",method=RequestMethod.POST)

public String handleNoSuchRequestHandlingMethod(){

System.out.println("handleNoSuchRequestHandlingMethod....");

return "success";

}

而客户端发送的是get请求:

handleNoSuchRequestHandlingMethod

1

这时候就会触发NoSuchRequestHandlingMethodException异常,返回给客户端一个就能过处理的页面:

4、SimpleMappingExceptionResolver

如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常

在springmvc中配置

error

处理方法

@RequestMapping("/testExceptionMapping")

public String testExceptionMapping(){

int arrays[] = new int[10];

System.out.println(arrays[11]);

return "hello";

}

很明显,访问这个处理方法是会产生数组下标越界异常,而上面配置了发生这个异常由error页面处理,并且将异常信息通过 ex 进行传递,所以在error.jsp页面:

${ex}

可以获取到异常信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值