基础信息提交
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user