SpringBoot 自定义全局异常处理实现

springboot 异常处理

关键字

  • 全局异常
  • 自定义异常
  • 自定义异常信息内容

1、自定义异常类

package com.example.learning.common.exception;

import lombok.Data;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;

/**
 * @author solin.jiang
 * @Description
 * @create 2020-12-02 14:20
 *
 * 自定义异常类
 */
@Data
public class LearningException extends RuntimeException {

    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String[] errorMsg;

    /**
     * 根据key构造带参数或不带参数的异常信息
     * @param key
     * @param values
     */
    public LearningException(String key, String... values) {
        this.errorCode = key;
        this.errorMsg = values;
    }

}

2、定义异常处理handler

package com.example.learning.common.exception;

import com.example.learning.common.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.MessageFormat;
import java.util.Locale;


/**
 * @author solin.jiang
 * @Description
 * @create 2020-12-02 14:15
 *
 * 全局异常处理
 *
 * SpringBoot中有一个@ControllerAdvice的注解,使用该注解表示开启了全局异常的捕获,
 * 只需在自定义一个方法使用@ExceptionHandler注解然后定义捕获异常的类型即可对这些捕获的异常进行统一的处理。
 * 但是还要得加上@ResponseBody注解,所以使用的是@RestControllerAdvice注解
 *
 * MessageFormat.format()可以实现this is {0},{1} demo. 格式模板
 */
@Slf4j
@RestControllerAdvice
public class BaseExceptionHandler {
    /**
     * 项目开发过程中的提示文字信息可以在资源文件中进行定义,而且资源文件是实现国际化技术的主要手段。
     * 如果想在SpringBoot里面进行资源文件的配置,只需要做一些简单的application.yml配置即可,
     * 而且所有注入的资源文件都可以像最初的Spring处理那样,直接使用MessageSource进行读取。
     *
     */
    @Autowired
    private MessageSource messageSource;

    /**
     * 处理自定义的业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = LearningException.class)
    public ResultVo LearningExceptionHandler(LearningException e){
        String message = messageSource.getMessage(e.getErrorCode(), null, Locale.getDefault());
        String format = MessageFormat.format(message, e.getErrorMsg());
        return ResultVo.error(format);
    }

}

3、添加配置

# 定义资源文件,多个资源文件使用逗号进行分割
spring:
  messages:
    basename: exception/error
    encoding: UTF-8

4、resource目录下创建error.properties

system.exception.500=系统异常{0},{1}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值