基础信息提交

This commit is contained in:
2024-10-30 20:04:37 +08:00
parent 8d7b1a34fe
commit 57441cc8df
16 changed files with 478 additions and 5 deletions

View File

@@ -0,0 +1,108 @@
package com.njcn.common.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.CharUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author hongawen
* @version 1.0.0
* @date 2021年04月21日 09:16
*/
public class ExceptionUtil {
private static final String PACKAGE_PREFIX = "com.njcn";
private static final String CONTROLLER = "controller";
/**
* 遍历出当前异常在本服务中栈堆信息----异常发生最初位置
* 本服务所有的包名均需要以com.njcn为前缀
* 该前缀不需要做配置管理,通常项目创建之初约定完便可。
*/
public static String getExceptionServerStackInfo(Exception exception) {
StackTraceElement[] stackTrace = exception.getStackTrace();
List<StackTraceElement> originalStackTraceElements = Arrays.asList(stackTrace);
//遍历出本服务的栈堆信息
List<StackTraceElement> stackTraceElements = originalStackTraceElements.stream().filter(stackInfo -> stackInfo.getClassName().contains(PACKAGE_PREFIX)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(stackTraceElements)) {
return stackTraceElements.get(0).toString();
} else if (CollectionUtil.isNotEmpty(originalStackTraceElements)) {
return originalStackTraceElements.get(0).toString();
}
return "未找到当前异常的堆栈信息";
}
/**
* 遍历出当前异常在本服务中栈堆信息----堆栈的所有信息
* 本服务所有的包名均需要以com.njcn为前缀
* 该前缀不需要做配置管理,通常项目创建之初约定完便可。
*/
public static List<String> getAllExceptionServerStackInfo(Exception exception) {
StackTraceElement[] stackTrace = exception.getStackTrace();
List<StackTraceElement> originalStackTraceElements = Arrays.asList(stackTrace);
//遍历出本服务的栈堆信息
List<StackTraceElement> stackTraceElements = originalStackTraceElements.stream().filter(stackInfo -> stackInfo.getClassName().contains(PACKAGE_PREFIX)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(stackTraceElements)) {
return stackTraceElements.stream().map(StackTraceElement::toString).collect(Collectors.toList());
} else if (CollectionUtil.isNotEmpty(originalStackTraceElements)) {
return originalStackTraceElements.stream().map(StackTraceElement::toString).collect(Collectors.toList());
}
List<String> noStackInfos = new ArrayList<>();
noStackInfos.add("未找到当前异常的堆栈信息");
return noStackInfos;
}
/**
* 遍历出当前异常堆栈的所有信息
* 本服务所有的包名均需要以com.njcn为前缀
* 该前缀不需要做配置管理,通常项目创建之初约定完便可。
*/
public static List<String> getAllExceptionStackInfo(Exception exception) {
StackTraceElement[] stackTrace = exception.getStackTrace();
List<StackTraceElement> originalStackTraceElements = Arrays.asList(stackTrace);
if (CollectionUtil.isNotEmpty(originalStackTraceElements)) {
return originalStackTraceElements.stream().map(StackTraceElement::toString).collect(Collectors.toList());
}
List<String> noStackInfos = new ArrayList<>();
noStackInfos.add("未找到当前异常的堆栈信息");
return noStackInfos;
}
/**
* 遍历异常信息栈获取信息栈中第一个出现的controller以及方法名称
* 用于反射获取注解内容,比如方法名,操作类型,操作严重度
* <p>
* 实际情况可能会出现没有controller入口的情况导致的异常比如网关gateway内所有请求
*/
public static List<String> getFirstControllerAndMethodName(Exception exception) {
List<String> controllerInfo = new ArrayList<>();
StackTraceElement[] stackTraces = exception.getStackTrace();
List<StackTraceElement> stackTraceElements = Stream.of(stackTraces)
.filter(stackInfo -> stackInfo.getClassName().contains(PACKAGE_PREFIX) && stackInfo.getClassName().contains(CONTROLLER))
.collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(stackTraceElements)) {
StackTraceElement stackTraceElement = stackTraceElements.get(0);
String stackInfo = stackTraceElement.toString();
String packageInfo = stackInfo.substring(stackInfo.indexOf(PACKAGE_PREFIX));
packageInfo = packageInfo.substring(0, packageInfo.indexOf("("));
String controllerName = packageInfo.substring(0, packageInfo.lastIndexOf(CharUtil.DOT));
String methodName = packageInfo.substring(packageInfo.lastIndexOf(CharUtil.DOT) + 1);
if(methodName.indexOf('$')>0){
methodName = methodName.substring(0, methodName.indexOf('$'));
}
Collections.addAll(controllerInfo, controllerName, methodName);
}
return controllerInfo;
}
}

View File

@@ -0,0 +1,54 @@
package com.njcn.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import java.util.List;
/**
* @author hongawen
* @version 1.0
* @data 2024/10/30 14:50
*/
@Slf4j
public class LogUtil {
/**
* 将判断是否开启debug模式抽取单独的方法必要时查看敏感信息
*
* @param log 日志输出器
*/
public static void njcnDebug(Logger log, String format, Object... args) {
if (log.isDebugEnabled()) {
log.info(format, args);
}
}
/**
* 将判断是否开启debug模式抽取单独的方法必要时查看debug级别信息
* 批量输出堆栈日志信息
*
* @param log 日志输出器
*/
public static void njcnPatchDebug(Logger log, List<String> stackInfos) {
if (log.isDebugEnabled()) {
stackInfos.forEach(log::error);
}
}
/**
* 区分是否开启debug模式输出系统异常日志信息
* 若开启debug模式则输出所有的堆栈信息
* 否则只输出第一行日志信息
*
* @param exception 异常
*/
public static void logExceptionStackInfo(String exceptionName, Exception exception) {
//若开启了debug模式则输出所有的栈堆信息
njcnPatchDebug(log, ExceptionUtil.getAllExceptionStackInfo(exception));
log.error("{}{},目标文件:{}",exceptionName, exception, ExceptionUtil.getExceptionServerStackInfo(exception));
}
}

View File

@@ -19,6 +19,7 @@
<mybatis.version>2.1.3</mybatis.version> <mybatis.version>2.1.3</mybatis.version>
<mybatis-plus.version>3.4.2</mybatis-plus.version> <mybatis-plus.version>3.4.2</mybatis-plus.version>
<jeffrey-mybatis-plus.version>1.5.1-RELEASE</jeffrey-mybatis-plus.version> <jeffrey-mybatis-plus.version>1.5.1-RELEASE</jeffrey-mybatis-plus.version>
<mybatis-plus-join.version>1.4.13</mybatis-plus-join.version>
<druid.version>1.2.5</druid.version> <druid.version>1.2.5</druid.version>
<mysql.version>8.0.19</mysql.version> <mysql.version>8.0.19</mysql.version>
</properties> </properties>
@@ -59,6 +60,13 @@
<version>${jeffrey-mybatis-plus.version}</version> <version>${jeffrey-mybatis-plus.version}</version>
</dependency> </dependency>
<!-- mybatis-plus增强增加多表join操作 -->
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>${mybatis-plus-join.version}</version>
</dependency>
<!--druid连接池--> <!--druid连接池-->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>

View File

@@ -7,6 +7,7 @@ import com.njcn.db.mybatisplus.handler.AutoFillValueHandler;
import com.njcn.db.mybatisplus.handler.BatchInjector; import com.njcn.db.mybatisplus.handler.BatchInjector;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
/** /**
* @author hongawen * @author hongawen
@@ -41,7 +42,7 @@ public class MybatisConfig {
* @author hongawen * @author hongawen
* @return BatchInjector * @return BatchInjector
*/ */
@Bean @Order(999)
public BatchInjector BatchInjector() { public BatchInjector BatchInjector() {
return new BatchInjector(); return new BatchInjector();
} }

View File

@@ -0,0 +1,5 @@
#### 统一springboot依赖版本包括
* springboot
* spring MVC
* swagger

View File

@@ -58,7 +58,6 @@
<version>${spring-boot.version}</version> <version>${spring-boot.version}</version>
</dependency> </dependency>
<!--API接口文档--> <!--API接口文档-->
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
@@ -71,6 +70,7 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId> <artifactId>knife4j-spring-ui</artifactId>

View File

@@ -0,0 +1,22 @@
package com.njcn.web.controller;
import com.njcn.web.utils.ReflectCommonUtil;
/**
* @author hongawen
* @version 1.0.0
*/
public class BaseController {
/**
* 获取当前类指定方法上@ApiOperate内容
* @param methodName 方法名
*/
public String getMethodDescribe(String methodName){
return ReflectCommonUtil.getMethodDescribeByClassAndMethodName(this.getClass(),methodName);
}
}

View File

@@ -0,0 +1,29 @@
package com.njcn.web.factory;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.web.pojo.param.BaseParam;
/**
* @author hongawen
* @version 1.0
* @data 2024/10/30 15:10
*/
public class PageFactory {
/**
* 默认第一页
* @param baseParam 查询参数
*/
public static Integer getPageNum(BaseParam baseParam) {
return ObjectUtil.isNull(baseParam.getPageNum()) || baseParam.getPageNum() == 0 ? 1 : baseParam.getPageNum();
}
/**
* 默认10条记录
* @param baseParam 查询参数
*/
public static Integer getPageSize(BaseParam baseParam) {
return ObjectUtil.isNull(baseParam.getPageSize()) || baseParam.getPageSize() == 0 ? 10 : baseParam.getPageSize();
}
}

View File

@@ -1,4 +1,4 @@
package com.njcn.common.pojo.dto; package com.njcn.web.pojo.dto;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

View File

@@ -1,4 +1,4 @@
package com.njcn.common.pojo.dto; package com.njcn.web.pojo.dto;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;

View File

@@ -0,0 +1,32 @@
package com.njcn.web.pojo.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @author hongawen
* @version 1.0.0
* @date 2022年03月18日 12:30
*/
@Data
public class SimpleDTO implements Serializable {
@ApiModelProperty(name = "name", value = "名称")
private String name;
@ApiModelProperty(name = "id", value = "索引")
private String id;
@ApiModelProperty(name = "code", value = "编码")
private String code;
@ApiModelProperty(name = "value", value = "数值")
private String value;
private Integer sort;
private Integer algoDescribe;
}

View File

@@ -0,0 +1,20 @@
package com.njcn.web.pojo.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @author hongawen
* @version 1.0.0
* @date 2022年03月24日 16:02
*/
@Data
public class SimpleTreeDTO extends SimpleDTO implements Serializable {
private List<SimpleDTO> children;
}

View File

@@ -1,4 +1,4 @@
package com.njcn.common.pojo.dto; package com.njcn.web.pojo.dto;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

View File

@@ -0,0 +1,43 @@
package com.njcn.web.pojo.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @author hongawen
* @version 1.0.0
* @date 2021年12月20日 15:35
*/
@Data
public class BaseParam implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("搜索值")
private String searchValue;
@ApiModelProperty("开始时间")
private String searchBeginTime;
@ApiModelProperty("结束时间")
private String searchEndTime;
@ApiModelProperty("状态")
private Integer searchState;
@ApiModelProperty("排序字段")
private String sortBy;
@ApiModelProperty("排序方式:asc-升序,desc-降序")
private String orderBy;
@ApiModelProperty("页码")
private Integer pageNum;
@ApiModelProperty("页面尺寸")
private Integer pageSize;
}

View File

@@ -0,0 +1,45 @@
package com.njcn.web.utils;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
/**
* @author hongawen
* @version 1.0
* @data 2024/10/30 14:51
*/
public class HttpResultUtil {
/**
* 组装结果集
*/
public static <T> HttpResult<T> assembleResult(String code, T result, String message) {
HttpResult<T> httpResult = new HttpResult<>();
httpResult.setCode(code);
httpResult.setMessage(message);
httpResult.setData(result);
return httpResult;
}
/**
* 组装通用结果集
*/
public static <T> HttpResult<T> assembleCommonResponseResult(CommonResponseEnum responseEnum, T result, String methodDescribe) {
String message = responseEnum.getMessage();
if (responseEnum.equals(CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION)) {
message = (String) result;
}
return assembleResult(responseEnum.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, message));
}
/**
* 业务异常组装结果集
*/
public static <T> HttpResult<T> assembleBusinessExceptionResult(BusinessException businessException, T result, String methodDescribe) {
return assembleResult(businessException.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, businessException.getMessage()));
}
}

View File

@@ -0,0 +1,106 @@
package com.njcn.web.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.LogInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.utils.ExceptionUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
/**
* @author hongawen
*/
@Slf4j
public class ReflectCommonUtil {
/**
* 根据异常获取系统内controller中的方法
* @param exception 运行时异常
*/
public static Method getMethod(Exception exception) {
List<String> stackInfo = ExceptionUtil.getFirstControllerAndMethodName(exception);
Method method = null;
if (!CollectionUtil.isEmpty(stackInfo)) {
String controllerName = stackInfo.get(0);
String methodName = stackInfo.get(1);
try {
method = ReflectUtil.getMethodByName(Class.forName(controllerName), methodName);
} catch (ClassNotFoundException e) {
log.error("根据controller名以及方法名反射获取方法体异常controller为{},方法为:{},异常为:{}", controllerName, methodName, e.getMessage());
}
}
return method;
}
/**
* 从异常堆栈信息里找出controller上@ApiOperation内容
*
* @param exception 异常
*/
public static String getMethodDescribeByException(Exception exception) {
List<String> stackInfo = ExceptionUtil.getFirstControllerAndMethodName(exception);
String operate = LogInfo.UNKNOWN_OPERATE;
if (!CollectionUtil.isEmpty(stackInfo)) {
String controllerName = stackInfo.get(0);
String methodName = stackInfo.get(1);
try {
operate = ReflectCommonUtil.getMethodDescribeByClassAndMethodName(Class.forName(controllerName), methodName);
} catch (ClassNotFoundException e) {
log.error("根据controller名以及方法名获取操作注解内容异常controller为{},方法为:{},异常为:{}", controllerName, methodName, e.getMessage());
}
}
return operate;
}
/**
* 获取controller上方法@ApiOperation注解的值作为当前方法的简短描述
*/
public static String getMethodDescribeByClassAndMethodName(Class<?> clazz, String methodName) {
Method method = ReflectUtil.getMethodByName(clazz, methodName);
return getMethodDescribeByMethod(method);
}
/**
* 获取方法上@ApiOperation注解的值作为当前方法的简短描述
*/
public static String getMethodDescribeByMethod(Method method) {
String operate = LogInfo.UNKNOWN_OPERATE;
if (Objects.nonNull(method) && method.isAnnotationPresent(ApiOperation.class)) {
operate = method.getAnnotation(ApiOperation.class).value();
}
return operate;
}
/**
* 根据controller上的方法@OperateInfo注解的内容返回当前[业务类型]以及[严重度]
*/
public static LogEnum getOperateInfoByMethod(Method method) {
LogEnum logEnum = LogEnum.BUSINESS_COMMON;
if(Objects.nonNull(method) && method.isAnnotationPresent(OperateInfo.class)){
logEnum = method.getAnnotation(OperateInfo.class).info();
}
return logEnum;
}
/**
* 根据controller上的方法@OperateInfo注解的内容返回当前操作类型
*/
public static String getOperateTypeByMethod(Method method) {
String operateType = OperateType.QUERY;
if(Objects.nonNull(method) && method.isAnnotationPresent(OperateInfo.class)){
operateType = method.getAnnotation(OperateInfo.class).operateType();
}
return operateType;
}
}