zoukankan      html  css  js  c++  java
  • GlobalExceptionHandler @ControllerAdvice

    package org.linlinjava.litemall.core.config;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.validator.internal.engine.path.PathImpl;
    import org.linlinjava.litemall.core.util.ResponseUtil;
    import org.springframework.core.annotation.Order;
    import org.springframework.http.converter.HttpMessageNotReadableException;
    import org.springframework.web.bind.MissingServletRequestParameterException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
    
    import javax.validation.ConstraintViolation;
    import javax.validation.ConstraintViolationException;
    import javax.validation.ValidationException;
    import java.util.Set;
    
    @ControllerAdvice
    @Order
    public class GlobalExceptionHandler {
    
        private Log logger = LogFactory.getLog(GlobalExceptionHandler.class);
    
        @ExceptionHandler(IllegalArgumentException.class)
        @ResponseBody
        public Object badArgumentHandler(IllegalArgumentException e) {
            logger.error(e.getMessage(), e);
            return ResponseUtil.badArgumentValue();
        }
    
        @ExceptionHandler(MethodArgumentTypeMismatchException.class)
        @ResponseBody
        public Object badArgumentHandler(MethodArgumentTypeMismatchException e) {
            logger.error(e.getMessage(), e);
            return ResponseUtil.badArgumentValue();
        }
    
        @ExceptionHandler(MissingServletRequestParameterException.class)
        @ResponseBody
        public Object badArgumentHandler(MissingServletRequestParameterException e) {
            logger.error(e.getMessage(), e);
            return ResponseUtil.badArgumentValue();
        }
    
        @ExceptionHandler(HttpMessageNotReadableException.class)
        @ResponseBody
        public Object badArgumentHandler(HttpMessageNotReadableException e) {
            logger.error(e.getMessage(), e);
            return ResponseUtil.badArgumentValue();
        }
    
        @ExceptionHandler(ValidationException.class)
        @ResponseBody
        public Object badArgumentHandler(ValidationException e) {
            logger.error(e.getMessage(), e);
            if (e instanceof ConstraintViolationException) {
                ConstraintViolationException exs = (ConstraintViolationException) e;
                Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
                for (ConstraintViolation<?> item : violations) {
                    String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage();
                    return ResponseUtil.fail(402, message);
                }
            }
            return ResponseUtil.badArgumentValue();
        }
    
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public Object seriousHandler(Exception e) {
            logger.error(e.getMessage(), e);
            return ResponseUtil.serious();
        }
    }
  • 相关阅读:
    7-1 N个数求和
    3662. 最大上升子序列和
    树状数组
    堆优化Dijkstra java模板
    皮亚诺曲线距离
    最长公共子序列(计数问题)
    最小路径覆盖
    极角排序
    2619. 询问
    Hessian矩阵与局部极小值
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11299970.html
Copyright © 2011-2022 走看看