zoukankan      html  css  js  c++  java
  • springBoot基于Bean和Method参数校验,捕捉异常

    package com.wlb.jp.config;
    
    import com.wlb.jp.utils.ReturnType;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.util.CollectionUtils;
    import org.springframework.validation.BindException;
    import org.springframework.validation.ObjectError;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.validation.ConstraintViolation;
    import javax.validation.ConstraintViolationException;
    import java.util.List;
    import java.util.Set;
    
    
    /**
     * 全局异常处理器
     */
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        /**
         * 用来处理method validation异常
         * @param ex
         * @return
         */
        @ExceptionHandler(ConstraintViolationException.class)
        @ResponseBody
        public ReturnType resolveConstraintViolationException(ConstraintViolationException ex){
            String code = "";
            String msg = "";
            Object value = "";
            ReturnType returnType = new ReturnType(code, msg, value);
    
            Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
            if(!CollectionUtils.isEmpty(constraintViolations)) {
                StringBuilder msgBuilder = new StringBuilder();
                for(ConstraintViolation constraintViolation :constraintViolations){
                    msgBuilder.append(constraintViolation.getMessage()).append(",");
                }
                String errorMessage = msgBuilder.toString();
                if (errorMessage.length() > 1) {
                    errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
                }
                returnType.setMsg(errorMessage);
                return returnType;
            }
            returnType.setMsg(ex.getMessage());
            return returnType;
        }
    
    
        /**
         * 用来处理bean validation异常
         * @param ex
         * @return
         */
    //    @ExceptionHandler(MethodArgumentNotValidException.class)
        @ExceptionHandler(BindException.class)
        @ResponseBody
        public ReturnType resolveMethodArgumentNotValidException(BindException ex){
            String code = "";
            String msg = "";
            Object value = "";
            ReturnType returnType = new ReturnType(code, msg, value);
    
            List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
            if(!CollectionUtils.isEmpty(objectErrors)) {
                StringBuilder msgBuilder = new StringBuilder();
                for (ObjectError objectError : objectErrors) {
                    msgBuilder.append(objectError.getDefaultMessage()).append(",");
                }
                String errorMessage = msgBuilder.toString();
                if (errorMessage.length() > 1) {
                    errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
                }
                returnType.setMsg(errorMessage);
                return returnType;
            }
            returnType.setMsg(ex.getMessage());
            return returnType;
        }
    }
  • 相关阅读:
    读取指定文件夹中的指定类型文件
    Java中字符串比较的注意点
    Access数据库 更新 "延时" 现象
    sqlserver版本分类下载以及各个版本之间的区别是什么
    android.os.NetworkOnMainThreadException
    Android模拟器分辨率介绍
    DataReader 绑定DataGridView有两种方式
    安装完Linux Mint后,发现系统中竟没有中文输入法
    处理EXCEL11问题
    windowplayer播放列表属性
  • 原文地址:https://www.cnblogs.com/zhuo-zui/p/13171335.html
Copyright © 2011-2022 走看看