天天看點

SpringBoot-08-之統一化json輸出與自定義異常捕獲

為及時了解異常,以及避免傳回的json格式不統,将每次請求的結果都返還一樣的形式。

此處統一為:{"code":響應代号碼,"msg":資訊,"data":資料} 具體效果如下:

result--format.png

1.格式化請求傳回值Bean對象:toly1994.com.toly01.hander.result.ResultEnum
/**
 * 作者:張風捷特烈
 * 時間:2018/5/25:15:30
 * 郵箱:[email protected]
 * 說明:格式化請求傳回值Bean對象
 */
public class ResultBean<T> {
    private int code;
    private String msg;
    private T data;

    //三參構造 set get toString 省略......
}
           
2.使用枚舉類統一錯誤碼與錯誤資訊維護:toly1994.com.toly01.hander.result.ResultEnum
/**
 * 作者:張風捷特烈
 * 時間:2018/5/25:17:36
 * 郵箱:[email protected]
 * 說明:使用枚舉類統一錯誤碼與錯誤資訊維護
 */
public enum ResultEnum {
    SUCCESS(200,"操作成功"),
    EXCEPTION(500, ""),
    NULL_ATTR(101,"屬性為空");

    private int code;
    private String msg;

    ResultEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

           
3.結果處理類:toly1994.com.toly01.hander.result.ResultHandler
/**
 * 作者:張風捷特烈
 * 時間:2018/5/30:18:37
 * 郵箱:[email protected]
 * 說明:結果處理類
 */
public class ResultHandler {
    /**
     * 成功時将object對象轉化為ResultBean對象
     *
     * @param o
     * @return
     */
    public static ResultBean ok(Object o) {

        return new ResultBean(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), o);
    }

    /**
     * 使用枚舉列舉錯誤類型
     *
     * @param error
     * @return
     */
    public static ResultBean error(ResultEnum error) {
        return new ResultBean(error.getCode(), error.getMsg(), null);
    }

    public static ResultBean error(String e) {
        return new ResultBean(ResultEnum.EXCEPTION.getCode(), e, null);
    }
}
           
4.異常捕獲類:toly1994.com.toly01.hander.ExceptionHandle
/**
 * 異常捕獲類
 */
@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBean handle(Exception e) {

        if (e instanceof NullAttrException) {
            return ResultHandler.error(ResultEnum.NULL_ATTR);
        }

        return ResultHandler.error(e.getMessage());
    }
}

           
5.自定義異常:這裡自定義一個示範一下:toly1994.com.toly01.hander.exception.NullAttrException
/**
 * 作者:張風捷特烈
 * 時間:2018/5/25:17:14
 * 郵箱:[email protected]
 * 說明:屬性為空異常
 */
public class NullAttrException extends RuntimeException {
    private int code;
    private static String msg = ResultEnum.NULL_ATTR.getMsg();

    public NullAttrException() {
        super(msg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
           

控制器:toly1994.com.toly01.controller.ResultUnitController

/**
 * 作者:張風捷特烈
 * 時間:2018/7/15:21:53
 * 郵箱:[email protected]
 * 說明:結果傳回統一化控制器
 */

@RestController
public class ResultUnitController {
    @Autowired
    private SwordRepository mSwordRepository;

    /**
     * 查詢所有:傳回json字元串
     *
     * @return
     */
    @GetMapping(value = "/swords/findall")
    public ResultBean findAllToJson() {
        if (1 == 1) {
            throw new NullAttrException();//此處故意抛出自定義異常看看
        }
        return ResultHandler.ok(mSwordRepository.findAll());
    }
}
           
至此便可完成上圖的效果