SpringMVC重要注解(一)@ExceptionHandler和@ResponseStatus

这里写图片描述

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

这里写图片描述


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



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

这里写图片描述

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

这里写图片描述

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

这里写图片描述

这里写图片描述


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


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


这里写图片描述


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


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

这里写图片描述

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

这里写图片描述


     
     
  1. package com.somnus.exception;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  7. public class HttpStatusException extends RuntimeException {
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. public HttpStatusException() {
  12. super();
  13. }
  14.  
  15. public HttpStatusException(String message, Throwable cause) {
  16. super(message, cause);
  17. }
  18.  
  19. public HttpStatusException(String message) {
  20. super(message);
  21. }
  22.  
  23. public HttpStatusException(Throwable cause) {
  24. super(cause);
  25. }
  26.  
  27. }

    
    
  1. @Controller
  2. @RequestMapping(value = "status")
  3. public class ResponseStatusController {
  4.  
  5. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String status(@PathVariable(value = "id") Integer id){
  8. if(id % 2 != 0){
  9. throw new HttpStatusException();
  10. }
  11. return id.toString();
  12. }
  13. }



效果如下:

这里写图片描述

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


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


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

这里写图片描述



这里写图片描述

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

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


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

这里写图片描述

这里写图片描述

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

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


    这里写图片描述

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

    这里写图片描述

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



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

    这里写图片描述

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

    这里写图片描述

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

    这里写图片描述

    这里写图片描述

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


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


    这里写图片描述

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


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

    这里写图片描述

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

    这里写图片描述

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



    效果如下:

    这里写图片描述

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

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


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

    这里写图片描述



    这里写图片描述

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

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


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

    这里写图片描述

    这里写图片描述

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

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


    猜你喜欢

    转载自blog.csdn.net/zjttlance/article/details/81698601