1.前后端交互
2.dto:数据传输对象,不往数据库里存储,用来接收页面过来的数据
公共分页请求对象======
import lombok.Setter; /** * 公共分页请求对象 */ @Setter public class PageRequestDto { /** * 每页条数 */ protected Integer size; /** * 当前页 */ protected Long page; public Integer getSize() { if (this.size == null || this.size <= 0 || this.size > 100) { setSize(10); } return size; } public Long getPage() { if (this.page == null || this.page <= 0) { setPage(1L); } return page; } }
通用分页结果=====
1 /** 2 * 通用分页结果 3 * 4 * @param <T> 5 */ 6 @Data 7 @NoArgsConstructor 8 @EqualsAndHashCode(callSuper = true) 9 public class PageResponseResult<T> extends ResponseResult<T> implements Serializable { 10 /** 11 * 当前页 12 */ 13 private Long currentPage; 14 /** 15 * 每页条数 16 */ 17 private Integer size; 18 /** 19 * 总数 20 */ 21 private Long total; 22 23 public PageResponseResult(Long currentPage, Integer size, Long total) { 24 this.currentPage = currentPage; 25 this.size = size; 26 this.total = total; 27 } 28 29 public PageResponseResult(Long currentPage, Integer size, Long total, T data) { 30 this.currentPage = currentPage; 31 this.size = size; 32 this.total = total; 33 this.setData(data); 34 } 35 }
通用的结果返回类==========
1 import com.heima.common.enums.AppHttpCodeEnum; 2 import lombok.Data; 3 4 import java.io.Serializable; 5 6 7 /** 8 * 通用的结果返回类 9 * @param <T> 10 */ 11 @Data 12 public class ResponseResult<T> implements Serializable { 13 /** 14 * 返回码 15 */ 16 private Integer code; 17 /** 18 * 错误提示 19 */ 20 private String errorMessage; 21 /** 22 * 返回数据 23 */ 24 private T data; 25 26 public ResponseResult() { 27 this.code = AppHttpCodeEnum.SUCCESS.getCode(); 28 } 29 30 public ResponseResult(Integer code, T data) { 31 this.code = code; 32 this.data = data; 33 } 34 35 public ResponseResult(Integer code, String msg) { 36 this.code = code; 37 this.errorMessage = msg; 38 } 39 40 public static ResponseResult okResult() { 41 return okResult(null); 42 } 43 44 public static <T> ResponseResult okResult(T data) { 45 return new ResponseResult(AppHttpCodeEnum.SUCCESS.getCode(), data); 46 } 47 48 public static ResponseResult errorResult(int code, String msg) { 49 return new ResponseResult(code, msg); 50 } 51 52 public static ResponseResult errorResult(AppHttpCodeEnum enums) { 53 return new ResponseResult(enums.getCode(), enums.getErrorMessage()); 54 } 55 56 public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage) { 57 return new ResponseResult(enums.getCode(), errorMessage); 58 } 59 60 }
测试:
public enum GenderEnum {
NAN(1,"男"),
NV(0,"女"),
NO(-1,"未知");
int code;
String value;
GenderEnum(int code, String value) {
this.code = code;
this.value = value;
}
}
===相应状态码枚举类
1 public enum AppHttpCodeEnum { 2 3 // 成功段0 4 SUCCESS(0,"操作成功"), 5 // 登录段1~50 6 NEED_LOGIN(1,"需要登录后操作"), 7 LOGIN_PASSWORD_ERROR(2,"密码错误"), 8 // TOKEN50~100 9 TOKEN_INVALID(50,"无效的TOKEN"), 10 TOKEN_EXPIRE(51,"TOKEN已过期"), 11 TOKEN_REQUIRE(52,"TOKEN是必须的"), 12 // SIGN验签 100~120 13 SIGN_INVALID(100,"无效的SIGN"), 14 SIG_TIMEOUT(101,"SIGN已过期"), 15 // 参数错误 500~1000 16 PARAM_REQUIRE(500,"缺少参数"), 17 PARAM_INVALID(501,"无效参数"), 18 PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"), 19 SERVER_ERROR(503,"服务器内部错误"), 20 // 数据错误 1000~2000 21 DATA_EXIST(1000,"数据已经存在"), 22 AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"), 23 DATA_NOT_EXIST(1002,"数据不存在"), 24 // 数据错误 3000~3500 25 NO_OPERATOR_AUTH(3000,"无权限操作"), 26 NEED_ADMIND(3001,"需要管理员权限"); 27 28 int code; 29 String errorMessage; 30 31 AppHttpCodeEnum(int code, String errorMessage){ 32 this.code = code; 33 this.errorMessage = errorMessage; 34 } 35 36 public int getCode() { 37 return code; 38 } 39 40 public String getErrorMessage() { 41 return errorMessage; 42 } 43 }
标签:code,String,项目,errorMessage,ResponseResult,了解,01,public,size From: https://www.cnblogs.com/xwz0705/p/16794987.html