这个是比较通用简单的。
Result.java
package com.wgq.common.lang;
import lombok.Data;
import java.io.Serializable;
@Data
public class Result implements Serializable {
private int code;
private String msg;
private Object data;
public static Result succ(int code, String msg, Object data) {
Result r = new Result();
r.setCode(code);
r.setMsg(msg);
r.setData(data);
return r;
}
public static Result fail(int code, String msg, Object data) {
Result r = new Result();
r.setCode(code);
r.setMsg(msg);
r.setData(data);
return r;
}
public static Result succ(Object data) {
return succ(200, "操作成功", data);
}
public static Result fail(String msg) {
return fail(400, msg, null);
}
}
还有整合异常的情况,异常、异常枚举记录在其他文章
ApiRestResponse.java
package com.imooc.cloud.mall.practice.common.common; import com.imooc.cloud.mall.practice.common.exception.ImoocMallExceptionEnum; public class ApiRestResponse{ private Integer status; private String msg; private T data; private static final int OK_CODE = 10000; private static final String OK_MSG = "SUCCESS"; public ApiRestResponse(Integer status, String msg, T data) { this.status = status; this.msg = msg; this.data = data; } public ApiRestResponse(Integer status, String msg) { this.status = status; this.msg = msg; } public ApiRestResponse() { this(OK_CODE, OK_MSG); } public static ApiRestResponse success() { return new ApiRestResponse<>(); } public static ApiRestResponse success(T result) { ApiRestResponse response = new ApiRestResponse<>(); response.setData(result); return response; } public static ApiRestResponse error(Integer code, String msg) { return new ApiRestResponse<>(code, msg); } public static ApiRestResponse error(ImoocMallExceptionEnum ex) { return new ApiRestResponse<>(ex.getCode(), ex.getMsg()); } @Override public String toString() { return "ApiRestResponse{" + "status=" + status + ", msg='" + msg + ''' + ", data=" + data + '}'; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public static int getOkCode() { return OK_CODE; } public static String getOkMsg() { return OK_MSG; } }



