通用结果返回类 (model模块)
DTO
ResponseResult
package com.zs.model.common.dtos;
import com.alibaba.fastjson.JSON;
import com.zs.model.common.enums.AppHttpCodeEnum;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class ResponseResult<T> implements Serializable {
private String host;
private Integer code;
private String errorMessage;
private String message;
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) {
this.code = code;
this.errorMessage = msg;
}
public ResponseResult(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public ResponseResult(Integer code, String errorMessage, String message, T data) {
this.code = code;
this.errorMessage = errorMessage;
this.message = message;
this.data = data;
}
public ResponseResult(String host, Integer code, String errorMessage, String message, T data) {
this.host = host;
this.code = code;
this.errorMessage = errorMessage;
this.message = message;
this.data = data;
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.message = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public static ResponseResult okResult(AppHttpCodeEnum enums){
return setOkAppHttpCodeEnum(enums,enums.getMessage());
}
private static ResponseResult setOkAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
return okResult(enums.getCode(),message);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setOkAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMessage());
if(data!=null) {
result.setData(data);
}
return result;
}
public static ResponseResult okResult(AppHttpCodeEnum enums, String errorMessage){
return setOkAppHttpCodeEnum(enums,errorMessage);
}
public static ResponseResult errorResult(int code, String errMsg) {
ResponseResult result = new ResponseResult();
return result.error(code, errMsg);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.errorMessage = msg;
return this;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums){
return setErrAppHttpCodeEnum(enums,enums.getMessage());
}
private static ResponseResult setErrAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
return errorResult(enums.getCode(),message);
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage){
return setErrAppHttpCodeEnum(enums,errorMessage);
}
public static ResponseResult errorResultMessage(AppHttpCodeEnum enums, String errorMessage){
return setErrAppHttpCodeEnum(enums,errorMessage);
}
private static ResponseResult setErrMsgAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
return errorResult(enums.getCode(),message);
}
public static ResponseResult errorResult(int code,String msg,String errMsg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg,errMsg);
}
public ResponseResult<?> error(Integer code,String msg, String errMsg) {
this.code = code;
this.message = msg;
this.errorMessage = errMsg;
return this;
}
public static void main(String[] args) {
PageResponseResult responseResult = new PageResponseResult(1,5,50);
List list = new ArrayList();
list.add("itcast");
list.add("itheima");
responseResult.setData(list);
System.out.println(JSON.toJSONString(responseResult));
}
}
PageResponseResult
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResponseResult extends ResponseResult implements Serializable {
private Integer currentPage;
private Integer size;
private Integer total;
}
PageRequestDto
@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);
}
}
}
Enum
AppHttpCodeEnum
package com.zs.model.common.enums;
public enum AppHttpCodeEnum {
SUCCESS(0,"操作成功"),
NEED_LOGIN(1,"需要登录后操作"),
LOGIN_PASSWORD_ERROR(2,"密码错误"),
TOKEN_INVALID(50,"无效的TOKEN"),
TOKEN_EXPIRE(51,"TOKEN已过期"),
TOKEN_REQUIRE(52,"TOKEN是必须的"),
SIGN_INVALID(100,"无效的SIGN"),
SIG_TIMEOUT(101,"SIGN已过期"),
PARAM_REQUIRE(500,"缺少参数"),
PARAM_INVALID(501,"无效参数"),
PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
SERVER_ERROR(503,"服务器内部错误"),
DATA_EXIST(1000,"数据已经存在"),
AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
DATA_NOT_EXIST(1002,"数据不存在"),
NO_OPERATOR_AUTH(3000,"无权限操作"),
NEED_ADMIND(3001,"需要管理员权限");
int code;
String message;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.message = errorMessage;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}