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

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

package com.example.demo.bean;

/* *
 * Created by Ay on 2018/9/20
 */
//返回的格式类
public class ResultBean {
    //错误码
    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测试,无异常情况
SpringBoot统一异常处理及统一回复接口_第1张图片
异常测试
SpringBoot统一异常处理及统一回复接口_第2张图片

你可能感兴趣的:(Spring,Boot)