## 自定义restful接口响应体 #### RestResponse ##### 作用:响应体载体,响应内容都在此对象中存储,此类定义MSG枚举消息体 ```java @Data @Accessors(chain = true) @ToString @NoArgsConstructor public class RestResponse implements Serializable { private static boolean showError = true; public enum MSG { // 成功 SUCCESS(1, "SUCCESS"), // 失败 FAILURE(0, "FAILURE"), // 参数为空 NULL_PARAM(10000, "参数为空"), // 对象为空 NULL_OBJ(10001, "对象为空"), // 有数据为空 NO_DATA_BASE_EXISTS(10002, "数据在库中为空"), // 参数未找到 NO_EXISTS_PARAM(10003, "参数未找到"), // 数据不存在 NO_DATA_EXISTS(10004, "数据不存在"), // 分页参数为空 NULL_PAGE_PARAM(10005, "分页参数为空"), // 参数验证失败 PARAM_VALIDATION_FAILURE(10006, "参数验证未通过"), // 参数不一致 PARAM_NOT_EQUAL(10007, "执行修改操作异常,参数id不一致"), // 参数验证失败 DATA_EXISTS(10008, "数据已存在"), // 参数值不符合规范 PARAM_VALUE_ERROR(11000, "参数值不符合规范"); int code; String msg; public int getCode() { return code; } public String getMsg() { return msg; } public void setCode(int code) { this.code = code; } public void setMsg(String msg) { this.msg = msg; } MSG(int code, String msg) { this.code = code; this.msg = msg; } } private int code; private String msg; @JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"}) private T data; public RestResponse(MSG msg) { this.code = msg.code; this.msg = msg.msg; } /** * 通过预设错误码和自定义错误信息创建 * @param code 错误码 * @param msg 错误信息 */ private RestResponse(int code, String msg) { this.code = code; this.msg = msg; } public RestResponse(T obj) { this.code = MSG.SUCCESS.code; this.msg = MSG.SUCCESS.msg; this.data = obj; } public RestResponse(MSG msg, T obj) { this.code = msg.code; this.msg = msg.msg; this.data = obj; } public RestResponse(int code,String msg, T obj) { this.code = code; this.msg = msg; this.data = obj; } public static RestResponse success() { return new RestResponse<>(MSG.SUCCESS); } public static RestResponse success(T obj) { return new RestResponse<>(MSG.SUCCESS, obj); } public static RestResponse success(int code, String msg) { return new RestResponse<>(code, msg); } public static RestResponse success(T obj,String msg) { return new RestResponse<>(MSG.SUCCESS.code,msg, obj); } public static RestResponse failure() { return new RestResponse<>(MSG.FAILURE); } public static RestResponse failure(int code, String msg) { return new RestResponse<>(code, msg); } public static RestResponse failure(MSG e) { return new RestResponse<>(e.getCode(), e.getMsg()); } public static RestResponse failure(MyException e) { return new RestResponse<>(e.getCode(), e.getMsg()); } public static RestResponse failure(T obj) { return new RestResponse<>(MSG.FAILURE, obj); } /** * 分页信息 * @param content 内容列表 * @param page 当前页数 * @param size 每页数据量 * @param total 数据总量 * @param totalPage 总页数 * @return res */ public static RestResponse> pageList(List content, long page, long size, long total, long totalPage) { PageResult pr = new PageResult<>(content, page, total, size, totalPage); return new RestResponse<>(MSG.SUCCESS, pr); } /** * 分页信息 * @param content 内容列表 * @param total 数据总量 * @return res */ public static RestResponse> simplePageList(List content, long total) { PageResult pr = new PageResult<>(content, total); return new RestResponse<>(MSG.SUCCESS, pr); } } ``` #### PageResult ##### 作用:分页查询响应体载体 ```java @Data @Builder @NoArgsConstructor @Accessors(chain = true) @ApiModel("分页返回对象") public class PageResult implements Serializable { private static final long serialVersionUID = 1402394526521183854L; @ApiModelProperty("主题内容") @JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"}) private List content; @ApiModelProperty("当前页") private long page; @ApiModelProperty("每页数量") private long size; @ApiModelProperty("总数量") private long total; @ApiModelProperty("总页数") private long totalPage; public PageResult(List content, long page, long size, long total, long totalPage){ this.content=content; this.page=page; this.size=size; this.totalPage=totalPage; this.total=total; } public PageResult(List content, long total){ this.content=content; this.total=total; } } ``` #### MyException ##### 作用:自定义异常类 ```java @Data @NoArgsConstructor @Accessors(chain = true) @Builder public class MyException extends RuntimeException { //错误码 private Integer code; //错误信息 private String msg; /** * 自定义异常信息 */ public MyException(Integer code, String msg) { super(msg); this.code = code; this.msg = msg; } /** * 响应类异常信息 */ public MyException(RestResponse.MSG msg) { super(msg.getMsg()); this.code=msg.getCode(); this.msg=msg.getMsg(); } public MyException append(String msg){ this.msg+=msg; return this; } public MyException before(String msg){ this.msg=msg+this.msg; return this; } } ```