不分頁通用返回
/**
* 通用的結(jié)果返回類
* @param <T>
*/
public class ResponseResult<T> implements Serializable {
private String host;
private Integer code;
private String errorMessage;
private T data;
public ResponseResult() {
this.code = 200;
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.errorMessage = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.errorMessage = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
if(data!=null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums){
return setAppHttpCodeEnum(enums,enums.getErrorMessage());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums,String errorMessage){
return setAppHttpCodeEnum(enums,errorMessage);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
return okResult(enums.getCode(),enums.getErrorMessage());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums,String errorMessage){
return okResult(enums.getCode(),errorMessage);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.errorMessage = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.errorMessage = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
分頁通用返回
public class PageResponseResult extends ResponseResult {
private Integer currentPage;
private Integer size;
private Integer total;
public PageResponseResult(Integer currentPage, Integer size, Integer total) {
this.currentPage = currentPage;
this.size = size;
this.total = total;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
通用請求dto
@Data
@Slf4j
public class PageRequestDto {
protected Integer size;
protected Integer page;
public void checkParam() {
if (this.page == null || this.page < 0) {
setPage(1);
}
if (this.size == null || this.size < 0 || this.size > 100) {
setSize(10);
}
}
}
通用的枚舉異常類
public enum AppHttpCodeEnum {
// 成功段0
SUCCESS(0,"操作成功"),
// 登錄段1~50
NEED_LOGIN(1,"需要登錄后操作"),
LOGIN_PASSWORD_ERROR(2,"密碼錯誤"),
// TOKEN50~100
TOKEN_INVALID(50,"無效的TOKEN"),
TOKEN_EXPIRE(51,"TOKEN已過期"),
TOKEN_REQUIRE(52,"TOKEN是必須的"),
// SIGN驗簽 100~120
SIGN_INVALID(100,"無效的SIGN"),
SIG_TIMEOUT(101,"SIGN已過期"),
// 參數(shù)錯誤 500~1000
PARAM_REQUIRE(500,"缺少參數(shù)"),
PARAM_INVALID(501,"無效參數(shù)"),
PARAM_IMAGE_FORMAT_ERROR(502,"圖片格式有誤"),
SERVER_ERROR(503,"服務(wù)器內(nèi)部錯誤"),
// 數(shù)據(jù)錯誤 1000~2000
DATA_EXIST(1000,"數(shù)據(jù)已經(jīng)存在"),
AP_USER_DATA_NOT_EXIST(1001,"ApUser數(shù)據(jù)不存在"),
DATA_NOT_EXIST(1002,"數(shù)據(jù)不存在"),
// 數(shù)據(jù)錯誤 3000~3500
NO_OPERATOR_AUTH(3000,"無權(quán)限操作");
int code;
String errorMessage;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.errorMessage = errorMessage;
}
public int getCode() {
return code;
}
public String getErrorMessage() {
return errorMessage;
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-556666.html
文章來源:http://www.zghlxwxcb.cn/news/detail-556666.html
到了這里,關(guān)于Java接口通用請求和響應(yīng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!