spring @ExceptionHandler 异常处理1

版权声明:欢迎交流、沟通 QQ:983433479 微信:wj983433479 ;努力学习~赚钱养家不是梦。 https://blog.csdn.net/u012881904/article/details/80616587

spring @ExceptionHandler 异常处理1

传统工程看异常的处理

这样的代码,我不知道你们是否使用过,反正我是使用过不少的,在大学学习spring的阶段,到后来到公司写代码,为了安全不管最后怎么样,catch异常少不了的,这个可能是自己不希望一个错误的异常界面出现在页面中。

@RequestMapping("/userInfoById")
@ResponseBody
public ActionResult getUserInofById(Integer id){
    ActionResult result = new ActionResult();
    try {
        result.setData(userInfoService.getUserInfoById(id));
    } catch (Exception e) {
        result.setSuccess(false);
        result.setMessage("error");
    }
    return result;
}

异常的改造项目

之前写的博客,使用Hibernate Validator 进行统一异常的校验处理Spring MVC Spring 中封装Hibernate Validator,简易使用校验,由于进行统一校验,少不了的异常的处理,那篇的博客中使用了两种方式去处理异常。
1、实现方式一通过@ExceptionHandler注解实现一个BaseAction,有各种的异常需要在方法体中不断的去处理。
2、实现方式二通过实现HandlerExceptionResolver接口,然后注入在spring容器中,然后通过直接将异常信息处理,然后打印到HttpServletResponse中去,返回null。

总结这两种的实现的方式都有缺点,对于特定的异常的处理都是需要通过代码中去实现,看起来非常的不爽,第一种实现方式,我也没有想过,使用的时候对于这个注解的了解不够深刻有点懵逼;对于第二种使用还是比较的清楚的,通过拦截到了异常,然后处理就行啦!有点n久之前在学习使用struts2项目中打印json,就是这么干的。不过还是有相同的缺点,对于特定异常处理的定位还是不够十分的明确!都是需要通过代码层面去控制处理。其实spring 支持我们这样的定义数据的,通过注解的形式,只是对于这个处理不是特别的了解而已,所以才会造成这样的尴尬的局面。

关于Spring @ExceptionHandler的介绍

关于spring@ExceptionHandler,官方的文档介绍
Spring HandlerExceptionResolver implementations deal with unexpected exceptions that occur during controller execution. 处理控制层出现的异常。
三种方法:
Besides implementing the HandlerExceptionResolver interface, which is only a matter of implementing the resolveException(Exception, Handler) method and returning a ModelAndView, you may also use the provided SimpleMappingExceptionResolver or create @ExceptionHandler methods.
1、SimpleMappingExceptionResolver 允许您使用可能抛出的任何异常的类名,并将其映射到视图名称。这在功能上等同于servlet API的异常映射特性,但也可以实现来自不同处理程序的异常细粒度映射。(SimpleMappingExceptionResolver enables you to take the class name of any exception that might be thrown and map it to a view name. This is functionally equivalent to the exception mapping feature from the Servlet API, but it is also possible to implement more finely grained mappings of exceptions from different handlers. )
2、@ExceptionHandler 另一方面,注释可用于应调用以处理异常的方法,这些方法可以局部的定义在 @Controller中或者可以在@ControllerAdvice类中定义时应用于许多@Controller类。@ControllerAdvice 是全局的,@Controller是局部的,如果局部的不能处理,才跑去全局处理哦。

3、The DefaultHandlerExceptionResolver translates Spring MVC exceptions to specific error status codes. It is registered by default with the MVC namespace, the MVC Java config, and also by the DispatcherServlet (i.e. when not using the MVC namespace or Java config). Listed below are some of the exceptions handled by this resolver and the corresponding status codes:
BindException 400 (Bad Request)
ConversionNotSupportedException 500 (Internal Server Error)
HttpMediaTypeNotAcceptableException 406 (Not Acceptable)
HttpMediaTypeNotSupportedException 415 (Unsupported Media Type)

参考文档-对于全局Advice的理解

其实这些参考文档的表达对于spring内部的实现的细节还不是特别的清楚,后面的有时间慢慢的了解,这里记录一下,不过从使用上来说的话!基本上满足了我们的基本的需求,一般使用注解来说,局部的大于全局,多个全局按照Order排序进行处理。
@ControllerAdvice 和 @ExceptionHandler 的区别
SpringMvc4.x基本配置(三):@ControllerAdvice注解
spring的ControllerAdvice注解用法
Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
使用@ControllerAdvice + @ExceptionHandler 注解实现Controller层异常全局处理

总结

从项目中的小角度去发现和总结,你会觉得自己的了解还不够深入,真正的深入的了解才可以游刃有余的处理我们项目中的数据哦!这些简单的工具对于我们的项目开发非常有用哦!非常的赞!

猜你喜欢

转载自blog.csdn.net/u012881904/article/details/80616587
今日推荐