备忘录 @RestControllerAdvice与异常拦截类示例


import com.mine.ResponseMO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;

/**
 * 统一异常处理基类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 判断是否是dev环境
     */
    protected boolean dev;

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = {Exception.class, RuntimeException.class})
    public ResponseMO defaultErrorHandler(Exception e) {
        logger.error(e.getMessage(), e);
        String debugInfo = null;
        if (dev) {
            debugInfo = e.toString();
        }
        return ResponseMO.error("内部发生错误,请联系管理员", debugInfo);
    }


    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class
            , MissingServletRequestParameterException.class})
    public ResponseMO methodArgumentNotValidException(Exception e) {
        if (logger.isDebugEnabled()) {
            logger.info(e.getMessage(), e);
        }

        //参数缺失异常
        if (e instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException exception = (MissingServletRequestParameterException) e;
            String message = exception.getParameterName() + "不能为空";
            return ResponseMO.error(message);
        }

        List<ObjectError> allErrors = new ArrayList<>();
        if (e instanceof BindException) {
            allErrors = ((BindException) e).getBindingResult().getAllErrors();
        } else if (e instanceof MethodArgumentNotValidException) {
            allErrors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
        }
        StringBuffer errors = new StringBuffer();
        for (ObjectError allError : allErrors) {
            errors.append(allError.getDefaultMessage());
            break;
        }

        String debugInfo = null;
        if (dev) {
            debugInfo = e.toString();
        }

        return ResponseMO.error(errors.toString(), debugInfo);
    }

    /**
     * 请求方法不支持异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseMO methodNotSupported(HttpRequestMethodNotSupportedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e.getMessage(), e);
        }
        String message = "不支持" + e.getMethod() + "请求访问";

        String debugInfo = null;
        if (dev) {
            debugInfo = e.toString();
        }
        return ResponseMO.error(message, debugInfo);
    }

    /**
     * 请求内容不支持
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseMO httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e.getMessage(), e);
        }
        String message = "不支持'" + e.getContentType() + "'内容";

        String debugInfo = null;
        if (dev) {
            debugInfo = e.toString();
        }
        return ResponseMO.error(message, debugInfo);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值