首页 > 编程语言 > 详细

java开发中简单又不容易的全局异常处理

时间:2021-07-18 23:25:07      阅读:24      评论:0      收藏:0      [点我收藏+]
  1 @ControllerAdvice
  2 public class ControllerExceptionHandler {
  3    //日志
  4     private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
  5 
  6     /**
  7      * 处理运行时的异常,可优化为自定义异常类
  8      */
  9     @ExceptionHandler(RuntimeException.class)
 10     @ResponseBody
 11     public ResponseEntity handlerException(RuntimeException ex) {
 12         log.error("发生业务异常!原因是:{}", ex.getMessage());
 13         return ResponseEntity.error(ex.getMessage());
 14     }
 15 
 16 
 17     /**
 18      * 对方法参数校验异常处理方法(仅对于表单提交有效,对于以json格式提交将会失效)
 19      * 如果是表单类型的提交,则spring会采用表单数据的处理类进行处理(进行参数校验错误时会抛出BindException异常)
 20      */
 21     @ExceptionHandler(BindException.class)
 22     @ResponseBody
 23     public ResponseEntity handlerBindException(BindException ex) {
 24         log.error("valid参数校验异常,提交参数类型form!原因是: {}", ex.getMessage());
 25         return handlerNotValidException(ex);
 26     }
 27 
 28     /**
 29      * 对方法参数校验异常处理方法(前端提交的方式为json格式出现异常时会被该异常类处理)
 30      * json格式提交时,spring会采用json数据的数据转换器进行处理(进行参数校验时错误是抛出MethodArgumentNotValidException异常)
 31      */
 32     @ExceptionHandler(MethodArgumentNotValidException.class)
 33     @ResponseBody
 34     public R handlerArgumentNotValidException(MethodArgumentNotValidException ex) {
 35         log.error("valid参数校验异常,提交参数类型Json!原因是: {}", ex.getMessage());
 36         return handlerNotValidException(ex);
 37     }
 38 
 39 
 40     @ExceptionHandler(Exception.class)
 41     @ResponseBody
 42     public ResponseEntity handlerException2(Exception ex) {
 43         log.error("发生未知异常!原因是: {}", ex.getMessage());
 44         return ResponseEntity.error(ex.getMessage());
 45     }
 46 
 47 
 48 
 49     /**
 50      * valid校验,其参数错误异常的统一处理
 51      * @param e
 52      */
 53     public ResponseEntity handlerNotValidException(Exception e) {
 54         log.debug("begin resolve argument exception");
 55         BindingResult result;
 56         if (e instanceof BindException) {
 57             BindException exception = (BindException) e;
 58             result = exception.getBindingResult();
 59         } else {
 60             MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
 61             result = exception.getBindingResult();
 62         }
 63 
 64 //        Map<String, Object> maps = getStringObjectMap(result);
 65         List<String> list = getStringObjectList(result);
 66         return ResponseEntity.error(list.toString());
 67     }
 68 
 69 
 70 
 71     /**
 72      * 这里只返回错误原因(建议)
 73      * 例子:[ID不能为空]
 74      */
 75     private List<String> getStringObjectList(BindingResult result) {
 76         List<String> list;
 77         if (result.hasErrors()) {
 78             List<FieldError> fieldErrors = result.getFieldErrors();
 79             list = new ArrayList<>(fieldErrors.size());
 80             fieldErrors.forEach(error -> {
 81                 list.add(error.getDefaultMessage());
 82             });
 83         } else {
 84             list = Collections.EMPTY_LIST;
 85         }
 86         return list;
 87     }
 88 
 89 
 90     /**
 91      * 这里是返回:字段名=错误原因(不建议)
 92      */
 93     private Map<String, Object> getStringObjectMap(BindingResult result) {
 94         Map<String, Object> maps;
 95         if (result.hasErrors()) {
 96             List<FieldError> fieldErrors = result.getFieldErrors();
 97             maps = new HashMap<>(fieldErrors.size());
 98             fieldErrors.forEach(error -> {
 99                 maps.put(error.getField(), error.getDefaultMessage());
100             });
101         } else {
102             maps = Collections.EMPTY_MAP;
103         }
104         return maps;
105     }
106 }

 

java开发中简单又不容易的全局异常处理

原文:https://www.cnblogs.com/chreat/p/14868211.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!