update 优化响应信息

This commit is contained in:
AprilWind
2026-03-18 13:47:49 +08:00
parent afdc8366f2
commit 7bc2315c64
3 changed files with 116 additions and 34 deletions

View File

@@ -29,17 +29,6 @@ public class PageResult<T> implements Serializable {
*/ */
private List<T> rows; private List<T> rows;
/**
* 分页
*
* @param list 列表数据
* @param total 总记录数
*/
public PageResult(List<T> list, long total) {
this.rows = list;
this.total = total;
}
/** /**
* 根据分页对象构建表格分页数据对象 * 根据分页对象构建表格分页数据对象
*/ */

View File

@@ -1,15 +1,19 @@
package org.dromara.common.core.domain; package org.dromara.common.core.domain;
import org.dromara.common.core.constant.HttpStatus;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.dromara.common.core.constant.HttpStatus;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import static org.dromara.common.core.constant.HttpStatus.ERROR;
import static org.dromara.common.core.constant.HttpStatus.SUCCESS;
/** /**
* 响应信息主体 * 响应信息主体
* *
* @param <T> 响应数据的泛型类型
* @author Lion Li * @author Lion Li
*/ */
@Data @Data
@@ -20,78 +24,152 @@ public class R<T> implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 成功 * 响应状态码
*/ */
public static final int SUCCESS = 200;
/**
* 失败
*/
public static final int FAIL = 500;
private int code; private int code;
/**
* 响应提示信息
*/
private String msg; private String msg;
/**
* 响应业务数据
*/
private T data; private T data;
/**
* 构建成功响应结果
*
* @param <T> 响应数据的泛型类型
* @return 成功响应结果对象
*/
public static <T> R<T> ok() { public static <T> R<T> ok() {
return restResult(null, SUCCESS, "操作成功"); return restResult(null, SUCCESS, "操作成功");
} }
/**
* 构建成功响应结果(带业务数据)
*
* @param data 业务数据
* @param <T> 响应数据的泛型类型
* @return 成功响应结果对象
*/
public static <T> R<T> ok(T data) { public static <T> R<T> ok(T data) {
return restResult(data, SUCCESS, "操作成功"); return restResult(data, SUCCESS, "操作成功");
} }
/**
* 构建成功响应结果(自定义提示信息)
*
* @param msg 自定义提示信息
* @param <T> 响应数据的泛型类型
* @return 成功响应结果对象
*/
public static <T> R<T> ok(String msg) { public static <T> R<T> ok(String msg) {
return restResult(null, SUCCESS, msg); return restResult(null, SUCCESS, msg);
} }
/**
* 构建成功响应结果(自定义提示信息+业务数据)
*
* @param msg 自定义提示信息
* @param data 业务数据
* @param <T> 响应数据的泛型类型
* @return 成功响应结果对象
*/
public static <T> R<T> ok(String msg, T data) { public static <T> R<T> ok(String msg, T data) {
return restResult(data, SUCCESS, msg); return restResult(data, SUCCESS, msg);
} }
/**
* 构建失败响应结果
*
* @param <T> 响应数据的泛型类型
* @return 失败响应结果对象
*/
public static <T> R<T> fail() { public static <T> R<T> fail() {
return restResult(null, FAIL, "操作失败"); return restResult(null, ERROR, "操作失败");
} }
/**
* 构建失败响应结果(自定义提示信息)
*
* @param msg 自定义提示信息
* @param <T> 响应数据的泛型类型
* @return 失败响应结果对象
*/
public static <T> R<T> fail(String msg) { public static <T> R<T> fail(String msg) {
return restResult(null, FAIL, msg); return restResult(null, ERROR, msg);
} }
/**
* 构建失败响应结果(带业务数据)
*
* @param data 业务数据
* @param <T> 响应数据的泛型类型
* @return 失败响应结果对象
*/
public static <T> R<T> fail(T data) { public static <T> R<T> fail(T data) {
return restResult(data, FAIL, "操作失败"); return restResult(data, ERROR, "操作失败");
} }
/**
* 构建失败响应结果(自定义提示信息+业务数据)
*
* @param msg 自定义提示信息
* @param data 业务数据
* @param <T> 响应数据的泛型类型
* @return 失败响应结果对象
*/
public static <T> R<T> fail(String msg, T data) { public static <T> R<T> fail(String msg, T data) {
return restResult(data, FAIL, msg); return restResult(data, ERROR, msg);
} }
/**
* 构建失败响应结果(自定义状态码+提示信息)
*
* @param code 自定义状态码
* @param msg 自定义提示信息
* @param <T> 响应数据的泛型类型
* @return 失败响应结果对象
*/
public static <T> R<T> fail(int code, String msg) { public static <T> R<T> fail(int code, String msg) {
return restResult(null, code, msg); return restResult(null, code, msg);
} }
/** /**
* 返回警告消息 * 构建警告响应结果
* *
* @param msg 返回内容 * @param msg 自定义提示信息
* @return 警告消息 * @param <T> 响应数据的泛型类型
* @return 警告响应结果对象
*/ */
public static <T> R<T> warn(String msg) { public static <T> R<T> warn(String msg) {
return restResult(null, HttpStatus.WARN, msg); return restResult(null, HttpStatus.WARN, msg);
} }
/** /**
* 返回警告消息 * 构建警告响应结果(自定义提示信息+业务数据)
* *
* @param msg 返回内容 * @param msg 自定义提示信息
* @param data 数据对象 * @param data 业务数据
* @return 警告消息 * @param <T> 响应数据的泛型类型
* @return 警告响应结果对象
*/ */
public static <T> R<T> warn(String msg, T data) { public static <T> R<T> warn(String msg, T data) {
return restResult(data, HttpStatus.WARN, msg); return restResult(data, HttpStatus.WARN, msg);
} }
/**
* 核心构建方法
*
* @param data 业务数据
* @param code 响应状态码
* @param msg 提示信息
* @param <T> 响应数据的泛型类型
* @return 响应结果对象
*/
private static <T> R<T> restResult(T data, int code, String msg) { private static <T> R<T> restResult(T data, int code, String msg) {
R<T> r = new R<>(); R<T> r = new R<>();
r.setCode(code); r.setCode(code);
@@ -100,11 +178,26 @@ public class R<T> implements Serializable {
return r; return r;
} }
/**
* 判断响应结果是否为失败
*
* @param ret 响应结果对象
* @param <T> 响应数据的泛型类型
* @return true=失败false=成功
*/
public static <T> Boolean isError(R<T> ret) { public static <T> Boolean isError(R<T> ret) {
return !isSuccess(ret); return !isSuccess(ret);
} }
/**
* 判断响应结果是否为成功
*
* @param ret 响应结果对象
* @param <T> 响应数据的泛型类型
* @return true=成功false=失败
*/
public static <T> Boolean isSuccess(R<T> ret) { public static <T> Boolean isSuccess(R<T> ret) {
return R.SUCCESS == ret.getCode(); return SUCCESS == ret.getCode();
} }
} }

View File

@@ -74,7 +74,7 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
wrapper.eq(FlowDefinition::getIsPublish, PublishStatus.PUBLISHED.getKey()); wrapper.eq(FlowDefinition::getIsPublish, PublishStatus.PUBLISHED.getKey());
Page<FlowDefinition> page = flowDefinitionMapper.selectPage(pageQuery.build(), wrapper); Page<FlowDefinition> page = flowDefinitionMapper.selectPage(pageQuery.build(), wrapper);
List<FlowDefinitionVo> list = BeanUtil.copyToList(page.getRecords(), FlowDefinitionVo.class); List<FlowDefinitionVo> list = BeanUtil.copyToList(page.getRecords(), FlowDefinitionVo.class);
return new PageResult<>(list, page.getTotal()); return PageResult.build(list, page.getTotal());
} }
/** /**
@@ -90,7 +90,7 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
wrapper.in(FlowDefinition::getIsPublish, Arrays.asList(PublishStatus.UNPUBLISHED.getKey(), PublishStatus.EXPIRED.getKey())); wrapper.in(FlowDefinition::getIsPublish, Arrays.asList(PublishStatus.UNPUBLISHED.getKey(), PublishStatus.EXPIRED.getKey()));
Page<FlowDefinition> page = flowDefinitionMapper.selectPage(pageQuery.build(), wrapper); Page<FlowDefinition> page = flowDefinitionMapper.selectPage(pageQuery.build(), wrapper);
List<FlowDefinitionVo> list = BeanUtil.copyToList(page.getRecords(), FlowDefinitionVo.class); List<FlowDefinitionVo> list = BeanUtil.copyToList(page.getRecords(), FlowDefinitionVo.class);
return new PageResult<>(list, page.getTotal()); return PageResult.build(list, page.getTotal());
} }
/** /**