Spring boot 2.0 全局异常

前言:该文章只是记录自己平时的点点滴滴.来激励自己成长.长成自己喜欢的人!

环境:

  1. 电脑 Mac 10.13.1
  2. IntelliJ IDEA 2018.2 (Ultimate Edition)
  3. Spring boot  2.0.1.RELEASE

实现方案:

  1. 各个微服务返回统一对象[异常,以及正常都返回一个bean]
  2. 可以通过某个异常定位到微服务

实现细节:

  1. @RestControllerAdvice  @ExceptionHandler(value = Exception.class) 
  2. BaseException
  3. IExceptionEnum -> TallyException
  4. ResultModel

@RestControllerAdvice  @ExceptionHandler(value = Exception.class) 


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ResultModel handler(Exception e){
        log.info("异常进入 ... 转换ResultModel ... ");
        if (e instanceof BaseException){
            BaseException iBaseException = (BaseException)e;
            return ResultModel.fail(iBaseException.getCode(), iBaseException.getMessage());
        } else {
            return ResultModel.fail(88888888,"88888888");
        }
    }
}

BaseException

public class BaseException extends RuntimeException{

    @Setter
    @Getter
    private int code;

    public BaseException(IExceptionEnum iExceptionEnum){
        super(iExceptionEnum.getMessage());
        this.code = iExceptionEnum.getCode();
    }
}

IExceptionEnum -> TallyException

public interface IExceptionEnum {

	/**code(key) */
	int getCode();

	/** message */
	String getMessage();

}
public class TallyExceptionEnum {

    public enum SYNC_EXCEPTION implements IExceptionEnum {
        SYNC_PROCESSING         (2001,"处理中,请稍后!!!"),
        SYNC_FAIL               (2002,"处理失败,请重试!!!"),
        SYNC_UPD_ACCOUNTDETAIL  (2006,"账户异常,请查证!!!");

        @Getter
        private int code;

        @Getter
        private String message;


        SYNC_EXCEPTION(int code, String message){
            this.code = code;
            this.message = message;
        }
    }
}

ResultModel

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultModel {

    public enum RESULT_STATUS_ENUM implements ICommonEnum {
        SUCCEED_CODE_VALUE               				(1000,"接口调用成功","1000");

        @Getter
        private String code;
        @Getter
        private Integer key;
        @Getter
        private String value;

        RESULT_STATUS_ENUM(int key, String value,String code){
            this.key = key;
            this.value = value;
            this.code = code;
        }
    }

	/**
     * 状态码
     */
    private int code;

    /**
     * 状态码描述
     */
    private String message;

    /**
     * 返回内容
     */
    private Object data;

    /**
     * 返回分页参数
     */
    private Object page;

    private static String typePage = "typePage";
    private static String typeData = "typeData";

    private ResultModel(int code,String message) {
        this.code = code;
        this.message = message;
    }

    private ResultModel(Object content, String type) {
        this.code = RESULT_STATUS_ENUM.SUCCEED_CODE_VALUE.getKey();
        this.message = RESULT_STATUS_ENUM.SUCCEED_CODE_VALUE.getValue();
        if (typePage.equals(type)){
            this.page = content;
        }
        if (typeData.equals(type)){
            this.data = content;
        }
    }

    public static ResultModel page(Object content) {
        return new ResultModel(content, typePage);
    }

    public static ResultModel data(Object content) {
        return new ResultModel(content, typeData);
    }

    public static ResultModel fail(int code,String message) {
        return new ResultModel(code,message);
    }

}

Demo

@RestController
@RequestMapping(value = "/tally")
@Slf4j
public class TallyController extends BaseController {


    /**
     * 异常demo
     * @author nmnl
     * @version 1.0.0
     * @date 2018-08-20 14:32:26
     */
    @PostMapping(value = "/rexception")
    public ResultModel RException(){
        throw new BaseException(TallyExceptionEnum.SYNC_EXCEPTION.SYNC_PROCESSING);
    }
}

注: pom如下

<dependencies>
        <!-- bean set / get model -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值