springboot全局异常配置

package com.tongsheng.common.advice;

import com.tongsheng.common.constant.Result;
import com.tongsheng.common.constant.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Yw
 * @version 1.0
 * @date 2019-12-25
 * @description 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    /**
     * shiro权限异常处理
     *
     * @return result
     */
    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuthorizationException() {
        return Result.getResult(ResultInfo.FORBIDDEN_ERROR);
    }

    /**
     * 处理参数异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public Result exceptionHandlerParamsException(IllegalArgumentException e) {
//        log.error("处理参数异常", e);
        return Result.getResult(ResultInfo.PARAME_EMPTY, e.getMessage());
    }


    /**
     * token无效异常
     */
    @ResponseBody
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Result handleTokenException(IncorrectCredentialsException incorrectCredentialsException) {
        log.error("token无效异常", incorrectCredentialsException);
        return Result.getResult(ResultInfo.TOKEN_ERROR);
    }


    /**
     * 参数校验异常处理
     *
     * @return result
     */
    @ResponseBody
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result handleMissingParameterException(MissingServletRequestParameterException missingServletRequestParameterException) {
        log.error("参数校验异常", missingServletRequestParameterException);
        return Result.getResult(ResultInfo.MISSING_PARAMS);
    }

    /**
     * 数字格式化异常处理
     *
     * @return result
     */
    @ResponseBody
    @ExceptionHandler(NumberFormatException.class)
    public Result handleNumberFormatException(NumberFormatException numberFormatException) {
        log.error("数字格式化异常", numberFormatException);
        return Result.getResult(ResultInfo.NUMBER_FORMAT_EXCEPTION);
    }


    /**
     * 请求方法错误异常
     *
     * @param e 请求方法错误异常
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return Result.getResult(ResultInfo.FAILED, "请求方法出错,你请求的方法" + e.getMethod() + ",该接口支持的方法" + e.getSupportedHttpMethods());
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("未知异常!原因是:", e);
        return Result.getResult(ResultInfo.SERVER_INTERNAL_EXCEPTION);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值