springboot全球异常捕获

全球异常捕获

全球异常捕获类GlobalExceptionCatch

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionCatch {
    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionCatch.class);

    @ExceptionHandler(Exception.class)
    public JSONObject serverExceptionHandler(final Exception exception) {
        JSONObject jsonObject = new JSONObject();
        if (exception instanceof WebException) {
            //自定义错误WebException
            WebException webException = (WebException) exception;
            logger.info("请求失败,状态码:{},错误信息:{}", webException.getMessageCode()
                    , SpringContext.getMessage(webException));
            jsonObject.put("result", "false");
            jsonObject.put("errorstr", SpringContext.getMessage(webException));
            jsonObject.put("errorcode", webException.getMessageCode().toString());
        } else {
            logger.info("请求失败,错误信息:", exception);
            jsonObject.put("result", "false");
            jsonObject.put("errorcode", "0");
            jsonObject.put("errorstr", "客服紧急处理中,请等待或联系管理员");
        }
        return jsonObject;
    }

}

WebException

public class WebException extends AbstractException {
    private static final long serialVersionUID = 1L;

    public WebException(Integer code, Object... params) {
        super(code, params);
    }
}

WebException

@Component
public class SpringContext implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);

    }

    public static < T > T getBean(Class< T > requiredType) throws BeansException {
        return applicationContext.getBean(requiredType);
    }

    public static < T > T getBean(String name, Class< T > requiredType) throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.isSingleton(name);
    }

    public static Class< ? > getType(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return applicationContext.getAliases(name);
    }

    public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        return applicationContext.getMessage(code, args, defaultMessage, locale);
    }

    public static String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
        return applicationContext.getMessage(code, args, locale);
    }

    public static String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
        return applicationContext.getMessage(resolvable, locale);
    }

    public static String getMessage(ServiceException serviceException) {
        return getMessage(String.valueOf(serviceException.getMessageCode()), serviceException.getMessageParams(), "", Locale.getDefault());
    }

    public static String getMessage(WebException webException) {
        return getMessage(String.valueOf(webException.getMessageCode()), webException.getMessageParams(), "", Locale.getDefault());
    }
}

对应的messages.properties信息

10010001 = 参数不能为空
10010002 = 抵用券不足
10010003 = 积分不足
10010004 = 支付记录不存在
#article-article
10020001 = 文章不存在
10020002 = 参数不能为空
10020003 = 文章已被删除
#article-channel
10021001 = 频道对象不存在
10021002 = 参数不能为空
10021003 = 还存在子频道或者文章
#comment
10030001 = 评论不存在
10030002 = 参数不能为空
#dealer
10040001 = 经销商对象不存在
10040002 = 参数不能为空
#favorite
10050001 = 用户收藏数据不存在
10050002 = 参数不能为空
#forum
10060001 = 论坛数据不存在
10060002 = 参数不能为空
10060003 = 帖子已删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值