增加 CommonResult 的 check 逻辑

This commit is contained in:
YunaiV
2022-06-17 23:54:42 +08:00
parent ff18d2f30a
commit ca28d791aa
38 changed files with 221 additions and 699 deletions

View File

@@ -1,33 +0,0 @@
package cn.iocoder.common.framework.util;
import java.util.Random;
public class MathUtil {
/**
* 随机对象
*/
private static final Random RANDOM = new Random(); // TODO 后续优化
/**
* 随机[min, max]范围内的数字
*
* @param min 随机开始
* @param max 随机结束
* @return 数字
*/
public static int random(int min, int max) {
if (min == max) {
return min;
}
if (min > max) {
int temp = min;
min = max;
max = temp;
}
// 随即开始
int diff = max - min + 1;
return RANDOM.nextInt(diff) + min;
}
}

View File

@@ -1,14 +0,0 @@
package cn.iocoder.common.framework.util;
import cn.hutool.system.SystemUtil;
/**
* 操作系统工具类
*/
public class OSUtils {
public static String getHostName() {
return SystemUtil.getHostInfo().getName();
}
}

View File

@@ -1,154 +0,0 @@
package cn.iocoder.common.framework.vo;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.exception.GlobalException;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.util.Assert;
import java.io.Serializable;
/**
* 通用返回
*
* @param <T> 数据泛型
*/
public final class CommonResult<T> implements Serializable {
/**
* 错误码
*
* @see ErrorCode#getCode()
*/
private Integer code;
/**
* 返回数据
*/
private T data;
/**
* 错误提示,用户可阅读
*
* @see ErrorCode#getMessage() ()
*/
private String message;
/**
* 错误明细,内部调试错误
*/
private String detailMessage;
/**
* 将传入的 result 对象,转换成另外一个泛型结果的对象
*
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
*
* @param result 传入的 result 对象
* @param <T> 返回的泛型
* @return 新的 CommonResult 对象
*/
public static <T> CommonResult<T> error(CommonResult<?> result) {
return error(result.getCode(), result.getMessage(), result.detailMessage);
}
public static <T> CommonResult<T> error(Integer code, String message) {
return error(code, message, null);
}
public static <T> CommonResult<T> error(Integer code, String message, String detailMessage) {
Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
CommonResult<T> result = new CommonResult<>();
result.code = code;
result.message = message;
result.detailMessage = detailMessage;
return result;
}
public static <T> CommonResult<T> success(T data) {
CommonResult<T> result = new CommonResult<>();
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
result.data = data;
result.message = "";
return result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getDetailMessage() {
return detailMessage;
}
public CommonResult<T> setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
return this;
}
@JSONField(serialize = false) // 避免序列化
public boolean isSuccess() {
return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code);
}
@JSONField(serialize = false) // 避免序列化
public boolean isError() {
return !isSuccess();
}
@Override
public String toString() {
return "CommonResult{" +
"code=" + code +
", data=" + data +
", message='" + message + '\'' +
", detailMessage='" + detailMessage + '\'' +
'}';
}
// ========= 和 Exception 异常体系集成 =========
/**
* 判断是否有异常。如果有,则抛出 {@link GlobalException} 或 {@link ServiceException} 异常
*/
public void checkError() throws GlobalException, ServiceException {
if (isSuccess()) {
return;
}
// 全局异常
if (GlobalErrorCodeConstants.isMatch(code)) {
throw new GlobalException(code, message).setDetailMessage(detailMessage);
}
// 业务异常
throw new ServiceException(code, message).setDetailMessage(detailMessage);
}
public static <T> CommonResult<T> error(ServiceException serviceException) {
return error(serviceException.getCode(), serviceException.getMessage(),
serviceException.getDetailMessage());
}
public static <T> CommonResult<T> error(GlobalException globalException) {
return error(globalException.getCode(), globalException.getMessage(),
globalException.getDetailMessage());
}
}

View File

@@ -1,46 +0,0 @@
package cn.iocoder.common.framework.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@ApiModel("分页参数")
public class PageParam implements Serializable {
@ApiModelProperty(value = "页码,从 1 开始", required = true,example = "1")
@NotNull(message = "页码不能为空")
@Min(value = 1, message = "页码最小值为 1")
private Integer pageNo;
@ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10")
@NotNull(message = "每页条数不能为空")
@Range(min = 1, max = 100, message = "条数范围为 [1, 100]")
private Integer pageSize;
public Integer getPageNo() {
return pageNo;
}
public PageParam setPageNo(Integer pageNo) {
this.pageNo = pageNo;
return this;
}
public Integer getPageSize() {
return pageSize;
}
public PageParam setPageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}
// public final int getOffset() {
// return (pageNo - 1) * pageSize;
// }
}

View File

@@ -1,36 +0,0 @@
package cn.iocoder.common.framework.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;
@ApiModel("分页结果")
public final class PageResult<T> implements Serializable {
@ApiModelProperty(value = "数据", required = true)
private List<T> list;
@ApiModelProperty(value = "总量", required = true)
private Long total;
public List<T> getList() {
return list;
}
public PageResult<T> setList(List<T> list) {
this.list = list;
return this;
}
public Long getTotal() {
return total;
}
public PageResult<T> setTotal(Long total) {
this.total = total;
return this;
}
}

View File

@@ -1,56 +0,0 @@
package cn.iocoder.common.framework.vo;
import java.io.Serializable;
/**
* 排序字段 DTO
*
* 类名加了 ing 的原因是,避免和 ES SortField 重名。
*/
public class SortingField implements Serializable {
/**
* 顺序 - 升序
*/
public static final String ORDER_ASC = "asc";
/**
* 顺序 - 降序
*/
public static final String ORDER_DESC = "desc";
/**
* 字段
*/
private String field;
/**
* 顺序
*/
private String order;
// 空构造方法,解决反序列化
public SortingField() {
}
public SortingField(String field, String order) {
this.field = field;
this.order = order;
}
public String getField() {
return field;
}
public SortingField setField(String field) {
this.field = field;
return this;
}
public String getOrder() {
return order;
}
public SortingField setOrder(String order) {
this.order = order;
return this;
}
}

View File

@@ -1,46 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>common</artifactId>
<groupId>cn.iocoder.mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-spring-boot-starter-system-error-code</artifactId>
<description>
错误码 ErrorCode 的自动配置功能,提供如下功能:
1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置;
2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码;
3. 自动写入:项目启动时,将项目本地的错误码写到 system-service 服务中,方便管理员在管理后台编辑;
</description>
<dependencies>
<!-- Mall 相关 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>system-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- RPC 相关 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -1,26 +0,0 @@
package cn.iocoder.mall.system.errorcode.config;
import cn.iocoder.mall.system.errorcode.core.ErrorCodeAutoGenerator;
import cn.iocoder.mall.system.errorcode.core.ErrorCodeRemoteLoader;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableConfigurationProperties(ErrorCodeProperties.class)
@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码
public class ErrorCodeAutoConfiguration {
// @Bean
// public ErrorCodeAutoGenerator errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
// return new ErrorCodeAutoGenerator(errorCodeProperties.getGroup())
// .setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
// }
//
// @Bean
// public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
// return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup());
// }
}

View File

@@ -1,39 +0,0 @@
package cn.iocoder.mall.system.errorcode.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
@ConfigurationProperties("mall.error-code")
@Validated
public class ErrorCodeProperties {
/**
* 应用分组
*/
@NotNull(message = "应用分组不能为空,请设置 mall.error-code.group 配置项,推荐直接使用 spring. application.name 配置项")
private String group;
/**
* 错误码枚举类
*/
private String constantsClass;
public String getGroup() {
return group;
}
public ErrorCodeProperties setGroup(String group) {
this.group = group;
return this;
}
public String getConstantsClass() {
return constantsClass;
}
public ErrorCodeProperties setConstantsClass(String constantsClass) {
this.constantsClass = constantsClass;
return this;
}
}

View File

@@ -1,84 +0,0 @@
package cn.iocoder.mall.system.errorcode.core;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ErrorCodeAutoGenerator {
//
// private Logger logger = LoggerFactory.getLogger(ErrorCodeAutoGenerator.class);
//
// /**
// * 应用分组
// */
// private final String group;
// /**
// * 错误码枚举类
// */
// private String errorCodeConstantsClass;
//
//
// @Autowired
// private ErrorCodeFeign errorCodeFeign;
// public ErrorCodeAutoGenerator(String group) {
// this.group = group;
// }
//
// public ErrorCodeAutoGenerator setErrorCodeConstantsClass(String errorCodeConstantsClass) {
// this.errorCodeConstantsClass = errorCodeConstantsClass;
// return this;
// }
//
// @EventListener(ApplicationReadyEvent.class)
// @Async // 异步,保证项目的启动过程,毕竟非关键流程
// public void execute() {
// // 校验 errorCodeConstantsClass 参数
// if (!StringUtils.hasText(errorCodeConstantsClass)) {
// logger.info("[execute][未配置 mall.error-code.constants-class 配置项,不进行自动写入到 system-service 服务]");
// return;
// }
// Class errorCodeConstantsClazz;
// try {
// errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
// } catch (ClassNotFoundException e) {
// logger.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
// return;
// }
// // 写入 system-service 服务
// logger.info("[execute][自动将 ({}) 类的错误码,准备写入到 system-service 服务]", errorCodeConstantsClass);
// List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
// Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
// if (field.getType() != ErrorCode.class) {
// return;
// }
// try {
// // TODO 芋艿:校验是否重复了;
// ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
// autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
// .setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
// } catch (IllegalAccessException e) {
// throw new RuntimeException(e);
// }
// });
// CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeFeign.autoGenerateErrorCodes(autoGenerateDTOs);
// if (autoGenerateErrorCodesResult.isSuccess()) {
// logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass);
// } else {
// logger.error("[execute][自动将 ({}) 类的错误码,失败写入到 system-service 服务,原因为 ({}/{}/{})]", errorCodeConstantsClass,
// autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMessage(), autoGenerateErrorCodesResult.getDetailMessage());
// }
// }
}

View File

@@ -1,70 +0,0 @@
package cn.iocoder.mall.system.errorcode.core;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.util.DateUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Date;
import java.util.List;
public class ErrorCodeRemoteLoader {
//
// private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
//
// private Logger logger = LoggerFactory.getLogger(ErrorCodeRemoteLoader.class);
//
// /**
// * 应用分组
// */
// private final String group;
//
// @Autowired
// private ErrorCodeFeign errorCodeFeign;
// private Date maxUpdateTime;
//
// public ErrorCodeRemoteLoader(String group) {
// this.group = group;
// }
//
// @EventListener(ApplicationReadyEvent.class)
// public void loadErrorCodes() {
// // 从 errorCodeFeign 全量加载 ErrorCode 错误码
// CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, null);
// listErrorCodesResult.checkError();
// logger.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
// // 写入到 ServiceExceptionUtil 到
// listErrorCodesResult.getData().forEach(errorCodeVO -> {
// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
// // 记录下更新时间,方便增量更新
// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
// });
// }
//
// @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
// public void refreshErrorCodes() {
// // 从 errorCodeFeign 增量加载 ErrorCode 错误码
// // TODO 优化点:假设删除错误码的配置,会存在问题;
// CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, maxUpdateTime);
// listErrorCodesResult.checkError();
// if (CollectionUtils.isEmpty(listErrorCodesResult.getData())) {
// return;
// }
// logger.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
// // 写入到 ServiceExceptionUtil 到
// listErrorCodesResult.getData().forEach(errorCodeVO -> {
// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
// // 记录下更新时间,方便增量更新
// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
// });
// }
}