开发中服务端对外服务需要定义完整的结构,如错误码、错码信息、数据等信息。下面包括普通 ServerResult、分页 ServerPageResult 的封装,开发者可自行根据调整。
服务端 ServerResult 封装
public class ServerResult<T> implements Serializable {
private T data;
private boolean success;
private int code;
private String message;
private ServerResult() {}
private ServerResult(boolean success, int code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
private ServerResult(T data, boolean success, int code, String message) {
this.data = data;
this.success = success;
this.code = code;
this.message = message;
}
/**
* 返回失败,code码和msg自定义
*/
public static <T> ServerResult<T> newInstance(){
return new ServerResult<T>();
}
/**
* 调用默认成功
*/
public static <T> ServerResult<T> defaultSuccess(T data){
return new ServerResult<T>(data, true, 200, "返回成功");
}
/**
* 返回默认失败
*/
public static <T> ServerResult<T> defaultFailure(){
return new ServerResult<T>(false, 500, "系统内部错误");
}
/**
* 自定义失败一
*/
public static <T> ServerResult<T> failure(T data, int code, String message){
return new ServerResult<T>(data, false, code, message);
}
/**
* 自定义失败二
*/
public static <T> ServerResult<T> failure(int code, String message){
return new ServerResult<T>(false, code, message);
}
public T getData() {
return data;
}
public ServerResult<T> data(T data) {
this.data = data;
return this;
}
public boolean isSuccess() {
return success;
}
public ServerResult<T> sucess(boolean success) {
this.success = success;
return this;
}
public int getCode() {
return code;
}
public ServerResult<T> code(int code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public ServerResult<T> message(String message) {
this.message = message;
return this;
}
}
服务端 ServerPageResult 封装
public class ServerPageResult<T> extends ServerResult<T> {
@Getter
@Setter
protected int pageNo = 1;
@Getter @Setter
protected int pageSize;
@Getter @Setter
protected int totalCount;
protected int totalPage;
public int getTotalPage() {
if(this.pageSize> 0 && this.totalCount >0) {
this.totalPage = this.totalCount % this.pageSize == 0 ? (this.totalCount / this.pageSize) : ( this.totalCount / this.pageSize + 1 );
} else {
this.totalPage = 0;
}
return totalPage;
}
public ServerPageResult<T> totalCount(int totalCount) {
setTotalCount(totalCount);
return this;
}
public ServerPageResult<T> pageSize(int pageSize) {
setPageSize(pageSize);
return this;
}
public ServerPageResult<T> pageNo(int pageNo) {
setPageNo(pageNo);
return this;
}
public static <T> ServerPageResult<T> defaultFailure(String errorCode, String errorMsg) {
ServerPageResult<T> result = new ServerPageResult<>();
result.setSuccess(false);
result.setMessage(errorMsg);
result.setCode(errorCode);
return result;
}
public static <T> ServerPageResult<T> defaultFailure(T data, String errorCode, String errorMsg) {
ServerPageResult<T> result = new ServerPageResult<>();
result.setSuccess(false);
result.setMessage(errorMsg);
result.setCode(errorCode);
result.setData(data);
return result;
}
public static <T> ServerPageResult<T> defaultSuccess(T data, Integer pageNo, Integer pageSize, Integer totalCount){
ServerPageResult<T> result = new ServerPageResult<>();
result.setSuccess(true);
result.setCode("200");
result.setData(data);
result.setPageNo(pageNo);
result.setPageSize(pageSize);
result.setTotalCount(totalCount);
return result;
}
}
测试用例
private ServerResult<Long> test() {
Long data = 1L;
/**
* 方式一:创建返回结果实例
*/
return ServerResult.<Long>newInstance().data(data);
/**
* 方式二:使用默认的
*/
return ServerResult.<Long>failure(data, 20, "msg");
/**
* 方式三
*/
ServerResult<Long> serverResult = ServerResult.newInstance();
return serverResult.sucess(true).code(200);
// 分页参数:成功返回分页参数
ServerPageResult<List<Student>> serverPageResult1 = ServerPageResult.defaultSuccess(studentList, 1, 20, 100);
// 分页参数:失败返回分页参数,无数据返回
ServerPageResult<List<Student>> serverPageResult2 = ServerPageResult.defaultFailure("GET_PAGE_ERROR", "获取分页信息失败");
// 分页参数:失败返回分页参数,默认数据返回
ServerPageResult<List<Student>> serverPageResult3 = ServerPageResult.defaultFailure(new ArrayList<>(),"GET_PAGE_ERROR", "获取分页信息失败");
}