参考:

http://blog.csdn.net/w372426096/article/details/78429132

http://blog.csdn.net/w372426096/article/details/78429141

@ExceptionHandler:统一处理某一类异常,从而能够减少代码重复率和复杂度

源码如下:

1 @Target({ElementType.METHOD})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 public @interface ExceptionHandler {
5 Class<? extends Throwable>[] value() default {};
6 }

注解作用对象为方法,并且在运行时有效,value()可以指定异常类。由该注解注释的方法可以具有灵活的输入参数(详细参见Spring API):

  • 异常参数:包括一般的异常或特定的异常(即自定义异常),如果注解没有指定异常类,会默认进行映射。
  • 请求或响应对象 (Servlet API or Portlet API): 你可以选择不同的类型,如ServletRequest/HttpServletRequest或PortleRequest/ActionRequest/RenderRequest
  • Session对象(Servlet API or Portlet API): HttpSession或PortletSession。
  • WebRequest或NativeWebRequest
  • Locale
  • InputStream/Reader
  • OutputStream/Writer
  • Model

方法返回值可以为:

  • ModelAndView对象
  • Model对象
  • Map对象
  • View对象
  • String对象
  • 还有@ResponseBody、HttpEntity<?>或ResponseEntity<?>,以及void

@ControllerAdvice

源码如下:

 1 @Target({ElementType.TYPE})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 @Component
5 public @interface ControllerAdvice {
6 @AliasFor("basePackages")
7 String[] value() default {};
8
9 @AliasFor("value")
10 String[] basePackages() default {};
11
12 Class<?>[] basePackageClasses() default {};
13
14 Class<?>[] assignableTypes() default {};
15
16 Class<? extends Annotation>[] annotations() default {};
17 }

该注解作用对象为TYPE,包括类、接口和枚举等,在运行时有效,并且可以通过Spring扫描为bean组件。其可以包含由@ExceptionHandler、@InitBinder 和@ModelAttribute标注的方法,可以处理多个Controller类,这样所有控制器的异常可以在一个地方进行处理

@ResponseStatus:可以将某种异常映射为HTTP状态码


无论是普通的WEB项目,还是用SpringMVC实现的restful服务,都曾经历过下面两个问题:

  1. @PostMapping(path = "/selectByAcctcode")
  2. public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
  3. log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
  4. MerAccountQueryResponse response = new MerAccountQueryResponse();
  5. try {
  6. Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
  7. response = merAccountService.selectByAcctcode(request, pageable);
  8. // 返回成功报文
  9. MessageUtil.createCommMsg(response);
  10. } catch (BizException e) {
  11. log.error(Constants.BUSINESS_ERROR, e);
  12. // 组织错误报文
  13. MessageUtil.errRetrunInAction(response, e);
  14. } catch (Exception ex) {
  15. log.error(Constants.EXCEPTION_ERROR, ex);
  16. // 组织错误报文
  17. MessageUtil.createErrorMsg(response,ex);
  18. }
  19. log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
  20. return response;
  21. }

当你有100个接口的时候,就得重复100次,如果你觉得代码量也就那么点,copy就copy吧,反正我是代码的搬运工,只是你有曾想过我可以抽取出来吗?

我们在写Controller的时候,如果没有出现过异常固然没问题,但一旦出现异常了,如果你处理了,那就需要你手动指定跳转到事先定义好的界面,如果你没处理,那将得到是一个非常丑陋的界面,如下:

如何避免这种问题呢???

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4. @ExceptionHandler({ ArithmeticException.class })
  5. public String handleArithmeticException(Exception e) {
  6. e.printStackTrace();
  7. return "error";
  8. }
  9. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  10. @ResponseBody
  11. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  12. System.out.println(10 / id);
  13. return id.toString();
  14. }
  15. }

当访问exception/e/0的时候,会抛出ArithmeticException异常,@ExceptionHandler就会处理并响应error.jsp

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4. @ExceptionHandler({ ArithmeticException.class })
  5. @ResponseBody
  6. public String handleArithmeticException(Exception e) {
  7. e.printStackTrace();
  8. JSONObject jo = new JSONObject();
  9. jo.put("resCode","999999");
  10. jo.put("resMsg","系统异常");
  11. return jo.toString();
  12. }
  13. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  14. @ResponseBody
  15. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  16. System.out.println(10 / id);
  17. return id.toString();
  18. }
  19. }

当然实际项目中,并不会像我这里写的这么简陋,我这里只是抛砖引玉,给你一个思路。

在实际项目中,可能碰到这种情况,我们提供的服务,调用方并不需要json报文中的消息,调用方只关注响应码,比如200,代表调用正常;404,代表请求资源不存在;502,代表系统异常。。。等等。我们又该如何去做?

  1. package com.somnus.exception;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.web.bind.annotation.ResponseStatus;
  4. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  5. public class HttpStatusException extends RuntimeException {
  6. private static final long serialVersionUID = 1L;
  7. public HttpStatusException() {
  8. super();
  9. }
  10. public HttpStatusException(String message, Throwable cause) {
  11. super(message, cause);
  12. }
  13. public HttpStatusException(String message) {
  14. super(message);
  15. }
  16. public HttpStatusException(Throwable cause) {
  17. super(cause);
  18. }
  19. }
  1. @Controller
  2. @RequestMapping(value = "status")
  3. public class ResponseStatusController {
  4. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  5. @ResponseBody
  6. public String status(@PathVariable(value = "id") Integer id){
  7. if(id % 2 != 0){
  8. throw new HttpStatusException();
  9. }
  10. return id.toString();
  11. }
  12. }

效果如下:

另外这里不得不提一点需要注意的,不要轻易把@ResponseStatus修饰目标方法,因为无论它执行方法过程中有没有异常产生,用户都会得到异常的界面,而目标方法正常执行。

  1. package com.somnus.controller;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.ResponseStatus;
  9. import com.somnus.exception.HttpStatusException;
  10. @Controller
  11. @RequestMapping(value = "status")
  12. public class ResponseStatusController {
  13. /**
  14. * ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
  15. * @param id
  16. * @return
  17. */
  18. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  19. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  20. @ResponseBody
  21. public String status2(@PathVariable(value = "id") Integer id){
  22. System.out.println(10 / id);
  23. return id.toString();
  24. }
  25. }

可以看到哪怕是响应2了,但是响应码其实还是502


如果我们要给jdk自带的异常提供一个响应码呢,我们又不可能去改源码,这时候@ResponseStatus就得配和@ControllerAdvice一起使用了,如下:

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4. @ExceptionHandler({ NullPointerException.class })
  5. @ResponseStatus(value=HttpStatus.NOT_FOUND)
  6. public void handleNullPointerException(Exception e) {
  7. e.printStackTrace();
  8. }
  9. @RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
  10. @ResponseBody
  11. public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
  12. List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
  13. return list.get(id);
  14. }
  15. }

当我们抛出NullPointerException异常的时候会发生什么呢

    • 当一个Controller中有多个@ExceptionHandler注解出现时,那么异常被哪个方法捕捉呢?这就存在一个优先级的问题,@ExceptionHandler的优先级是:在异常的体系结构中,哪个异常与目标方法抛出的异常血缘关系越紧密,就会被哪个捕捉到

    • @ExceptionHandler这个只会是在当前的Controller里面起作用,如果想在所有的Controller里面统一处理异常的话,可以用@ControllerAdvice来创建一个专门处理的类,我们在下一篇做介绍。

@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

  1. package org.springframework.web.bind.annotation;
  2. @Target(ElementType.TYPE)
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Component
  6. public @interface ControllerAdvice {
  7. @AliasFor("basePackages")
  8. String[] value() default {};
  9. @AliasFor("value")
  10. String[] basePackages() default {};
  11. Class<?>[] basePackageClasses() default {};
  12. Class<?>[] assignableTypes() default {};
  13. Class<? extends Annotation>[] annotations() default {};
 

没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

再一起看看官方提供的comment。

大致意思是:

  • @ControllerAdvice是一个@Component,用于定义@ExceptionHandler@InitBinder@ModelAttribute方法,适用于所有使用@RequestMapping方法。

  • Spring4之前,@ControllerAdvice在同一调度的Servlet中协助所有控制器。Spring4已经改变:@ControllerAdvice支持配置控制器的子集,而默认的行为仍然可以利用。

  • 在Spring4中, @ControllerAdvice通过annotations()basePackageClasses()basePackages()方法定制用于选择控制器子集。

不过据经验之谈,只有配合@ExceptionHandler最有用,其它两个不常用。

在SpringMVC重要注解(一)@ExceptionHandler@ResponseStatus我们提到,如果单使用@ExceptionHandler,只能在当前Controller中处理异常。但当配合@ControllerAdvice一起使用的时候,就可以摆脱那个限制了。

  1. package com.somnus.advice;
  2. import org.springframework.web.bind.annotation.ControllerAdvice;
  3. import org.springframework.web.bind.annotation.ExceptionHandler;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. @ControllerAdvice
  6. public class ExceptionAdvice {
  7. @ExceptionHandler({ ArrayIndexOutOfBoundsException.class })
  8. @ResponseBody
  9. public String handleArrayIndexOutOfBoundsException(Exception e) {
  10. e.printStackTrace();
  11. return "testArrayIndexOutOfBoundsException";
  12. }
  13. }
  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  5. @ResponseBody
  6. public String testExceptionHandle2(@PathVariable(value = "id") Integer id) {
  7. List<String> list = Arrays.asList(new String[]{"a","b","c","d"});
  8. return list.get(id-1);
  9. }
  10. }

当我们访问http://localhost:8080/SpringMVC/exception/e2/5的时候会抛出ArrayIndexOutOfBoundsException异常,这时候定义在@ControllerAdvice中的@ExceptionHandler就开始发挥作用了。

如果我们想定义一个处理全局的异常

  1. package com.somnus.advice;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.core.annotation.AnnotationUtils;
  4. import org.springframework.web.bind.annotation.ControllerAdvice;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.bind.annotation.ResponseStatus;
  8. @ControllerAdvice
  9. public class ExceptionAdvice {
  10. @ExceptionHandler({ Exception.class })
  11. @ResponseBody
  12. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  13. e.printStackTrace();
  14. return e.getMessage();
  15. }
  16. }

乍一眼看上去毫无问题,但这里有一个纰漏,由于Exception是异常的父类,如果你的项目中出现过在自定义异常中使用@ResponseStatus的情况,你的初衷是碰到那个自定义异常响应对应的状态码,而这个控制器增强处理类,会首先进入,并直接返回,不会再有@ResponseStatus的事情了,这里为了解决这种纰漏,我提供了一种解决方式。

  1. package com.somnus.advice;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.core.annotation.AnnotationUtils;
  4. import org.springframework.web.bind.annotation.ControllerAdvice;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.bind.annotation.ResponseStatus;
  8. @ControllerAdvice
  9. public class ExceptionAdvice {
  10. @ExceptionHandler({ Exception.class })
  11. @ResponseBody
  12. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  13. e.printStackTrace();
  14. //If the exception is annotated with @ResponseStatus rethrow it and let
  15. // the framework handle it - like the OrderNotFoundException example
  16. // at the start of this post.
  17. // AnnotationUtils is a Spring Framework utility class.
  18. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
  19. throw e;
  20. }
  21. // Otherwise setup and send the user to a default error-view.
  22. /*ModelAndView mav = new ModelAndView();
  23. mav.addObject("exception", e);
  24. mav.addObject("url", request.getRequestURL());
  25. mav.setViewName(DEFAULT_ERROR_VIEW);
  26. return mav;*/
  27. return e.getMessage();
  28. }
  29. }

如果碰到了某个自定义异常加上了@ResponseStatus,就继续抛出,这样就不会让自定义异常失去加上@ResponseStatus的初衷。

SpringMVC异常处理注解@ExceptionHandler@ControllerAdvice@ResponseStatus的更多相关文章

  1. springBoot注解大全JPA注解springMVC相关注解全局异常处理

    https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...

  2. SpringMVC重要注解 @ControllerAdvice

    @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现: package org.spring ...

  3. Spring中通过java的@Valid注解和@ControllerAdvice实现全局异常处理。

    通过java原生的@Valid注解和spring的@ControllerAdvice和@ExceptionHandler实现全局异常处理的方法: controller中加入@Valid注解: @Req ...

  4. SpringMVC异常处理机制详解[附带源码分析]

    目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...

  5. SpringMVC异常处理机制

    SpringMVC异常处理机制 springMVC会将所有在doDispatch方法中的异常捕获,然后处理.无法处理的异常会抛出给容器处理. 在doDispatch()中调用processDispat ...

  6. SpringMVC常用注解(三)

    一.@Controller .@RestController 和 @ControllerAdvice 1. @Controller @Controller 用于标记在一个类上,使用它标记的类就是一个S ...

  7. springmvc异常处理解析#ExceptionHandlerExceptionResolver

    开头 试想一下我们一般怎么统一处理异常呢,答:切面.但抛开切面不讲,如果对每一个controller方法抛出的异常做专门处理,那么着实太费劲了,有没有更好的方法呢?当然有,就是本篇文章接下来要介绍的s ...

  8. SpringMVC异常处理方式

    一.描述 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合 ...

  9. Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)

    Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...

随机推荐

  1. aircrack-ng套件学习笔记

    Aircrack-ng套件 1.airdecap-ng 该工具主要用于对加密无线数据报文的解码. 1.当无线网络启用了WEP或者WPA-PASK加密,可以使用wireshark过滤,过滤条件为:IEE ...

  2. C# 进程的挂起与恢复

    1. 源起: 仍然是模块化编程所引发的需求.产品经理难伺候,女产品经理更甚之~:p 纯属戏谑,技术方案与产品经理无关,芋头莫怪! VCU10项目重构,要求各功能模块以独立进程方式实现,比如:音视频转换 ...

  3. Eclipse通用设置

    分类 Eclipse分为64位.32位,安装版.免安装版 查看Eclipse版本信息 Help - About Eclipse - Installation Details

  4. mysql 原理~ index的详解

    一 简介:今天咱们来介绍下index的一些东西 二 数据的基本存储结构 1 磁盘空间被划分为许多大小相同的块(Block) 在内存中读出是页(Page).   2 一个表的这些数据块以链表的方式串联在 ...

  5. 「About Blockchain(一)」达沃斯年会上的区块链

    「About Blockchain(一)」 --达沃斯年会上的区块链 写在前面:1月23日到26日,在瑞士达沃斯召开了第48届世界经济论坛.这个新闻本没有引起我格外的关注,直到前两天张老师分享给我一篇 ...

  6. host-only

    https://www.cnblogs.com/yaox/p/6635312.html

  7. 嵌入式linux系统中,lsusb出现unable to initialize libusb: -99 解决办法 【转】

    转自:http://cpbest.blog.163.com/blog/static/41241519201111575726966/ libusb是linux系统中,提供给用户空间访问usb设备的AP ...

  8. 在Asp.Net Core中使用中间件保护非公开文件

    在企业开发中,我们经常会遇到由用户上传文件的场景,比如某OA系统中,由用户填写某表单并上传身份证,由身份管理员审查,超级管理员可以查看. 就这样一个场景,用户上传的文件只能有三种人看得见(能够访问) ...

  9. 百度AI—人脸在线比对

    首先访问百度AI网站:https://cloud.baidu.com/,按照下图的指示点开到应用管理的页面. 穿件完成之后到管理中可以查看到对应的 添加工具类: using System; using ...

  10. Sql 插入自定义主键

    在遇到数据库设计是自增的主键,且需要插入自定义的主键Id时,这个时候如果直接Insert的话,将会发生错误,错误提示信息: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'XXX' ...