SpringBoot统一异常处理及统一回复接口

SpringBoot及Mybait坏境搭建详见上一篇
一.定义回复接口格式类

package com.example.demo.bean;

/* *
 * Created by Ay on 2018/9/20
 */
//返回的格式类
public class ResultBean<T> {
    //错误码
    private int errCode;
    //错误信息
    private String message;
    //返回的数据
    private T data;

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

描述与前端交互的数据格式

二.定义回复结果工具类

package com.example.demo.utils;

import com.example.demo.bean.ResultBean;

/* *
 * Created by Ay on 2018/9/20
 */
public class ResultUtil {

    /*成功并返回数据*/
    public static ResultBean success(Object data){
        ResultBean resultBean = new ResultBean();
        resultBean.setErrCode(0);
        resultBean.setMessage("OK");
        resultBean.setData(data);
        return resultBean;
    }

    /*成功不返回数据*/
    public static ResultBean success(){
        return success(null);
    }

    /*失败并返回数据*/
    public static ResultBean error(Integer errorCode,String message){
        ResultBean resultBean = new ResultBean();
        resultBean.setErrCode(errorCode);
        resultBean.setMessage(message);
        resultBean.setData(null);
        return resultBean;

    }
}

三.自定义异常类

package com.example.demo.exception;

/* *
 * Created by Ay on 2018/9/20
 */
public class MyException extends RuntimeException {
    /* 异常码 */
    private Integer errorCode;
    /* 异常描述 */
    private String message;

    public MyException(Integer code, String msg){
        super(msg);
        this.errorCode = code;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }
}

四.统一异常处理类

package com.example.demo.exception;

import com.example.demo.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/* *
 * Created by Ay on 2018/9/20
 */
@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public Object errorHandler(MyException e){
        e.printStackTrace();
        return ResultUtil.error(e.getErrorCode(),e.getMessage());
    }
}

@ExceptionHandler指定要捕捉处理的异常类型
各层的业务逻辑代码遇到异常直接往上层抛出,最终将会在这个类进行统一处理。
五.测试
controller类加入测试方法

 @RequestMapping(value = "error/{id}")
    public Object errorTest(@PathVariable("id") Integer id) {
        if(id == 0){
            throw new MyException(-1,"除0");
        }
        int result = 10 / id;
        return ResultUtil.success(result);
    }

使用Postman测试,无异常情况
在这里插入图片描述
异常测试
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值