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();
        }
    }
  • 相关阅读:
    问题:Failed to execute goal org.apache.maven.plugins:mavencompilerplugin:3.5.1:compile (defaultcompile)
    STL容器的内存分配
    C++中的引用到底是什么
    解决eclipse无法解析shared_ptr
    Deleted pointer causes undefined behaviour
    诞生于饭桌上的jcSQL语言
    状压dp做题笔记
    TC做题笔记
    数据结构做题笔记
    POI做题笔记
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11299970.html
Copyright © 2011-2022 走看看