1. 建立 json 结果类,用于统一显示 json 结果
public class JsonResult<T> {
// code=0表示成功,返回数据data。data!=0,表示错误,返回错误信息msg
private int code;
// 若有错误,data不生成json
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
// 若无错误,msg 不生成 json
@JsonInclude(JsonInclude.Include.NON_NULL)
private String msg;
public JsonResult() {
code = 0;
}
public JsonResult<T> ok(T data) {
this.code = 0;
this.data = data;
return this;
}
public JsonResult<T> error(int code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
// getter and setter
}
2. 建立自定义异常类,用于显示自定义的异常
public class CustemerException extends Exception {
public CustemerException(String message) {
super