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();
        }
    }
  • 相关阅读:
    Python操作文件和目录
    ffmpeg命令简单使用
    【转载】一个简单的爬虫:爬取豆瓣的热门电影的信息
    【转载】正则表达式re.S的用法
    linux用户添加
    SQL语句update修改数据库字段
    linux命令之cp
    linux命令——tree命令
    Linux磁盘管理
    Python资源安装过程出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11299970.html
Copyright © 2011-2022 走看看