1. 程式人生 > >SpringBoot統一異常處理及統一回復介面

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測試,無異常情況
在這裡插入圖片描述
異常測試
在這裡插入圖片描述