项目初始化
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.joju.datamanager.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
@Documented
|
||||
public @interface EleganceLog {
|
||||
/**
|
||||
* 日志描述信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String description() default "";
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.joju.datamanager.common.annotation;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ResultAnnotation {
|
||||
boolean required() default true;
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package com.joju.datamanager.common.elegancelog;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.joju.datamanager.common.annotation.EleganceLog;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class EleganceLogAspect {
|
||||
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(EleganceLogAspect.class);
|
||||
/**
|
||||
* 换行符
|
||||
*/
|
||||
private static final String LINE_SEPARATOR = System.lineSeparator();
|
||||
|
||||
/**
|
||||
* 以自定义 @WebLog 注解为切点
|
||||
*/
|
||||
@Pointcut("@annotation(com.joju.datamanager.common.annotation.EleganceLog)")
|
||||
public void EleganceLog() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 在切点之前织入
|
||||
*
|
||||
* @param joinPoint
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Before("EleganceLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
// 开始打印请求日志
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
// 获取 @WebLog 注解的描述信息
|
||||
String methodDescription = getAspectLogDescription(joinPoint);
|
||||
|
||||
// 打印请求相关参数
|
||||
logger.info("========================================== Start ==========================================");
|
||||
// 打印请求 url
|
||||
logger.info("URL : {}", request.getRequestURL().toString());
|
||||
// 打印描述信息
|
||||
logger.info("Description : {}", methodDescription);
|
||||
// 打印 Http method
|
||||
logger.info("HTTP Method : {}", request.getMethod());
|
||||
// 打印调用 controller 的全路径以及执行方法
|
||||
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
|
||||
// 打印请求的 IP
|
||||
logger.info("IP : {}", request.getRemoteAddr());
|
||||
// 打印请求入参
|
||||
if (methodDescription.equals("loginUserInfo") || methodDescription.equals("quitUserInfo")) {
|
||||
|
||||
} else {
|
||||
|
||||
logger.info("Request Args : {}", JSONUtil.toJsonStr(joinPoint.getArgs()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 环绕
|
||||
*
|
||||
* @param proceedingJoinPoint
|
||||
* @return
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Around("EleganceLog()")
|
||||
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Object result = proceedingJoinPoint.proceed();
|
||||
// 打印出参
|
||||
|
||||
logger.info("Response Args : {}", JSONUtil.toJsonStr(result));
|
||||
// 执行耗时
|
||||
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
|
||||
logger.info("=========================================== End ===========================================" + LINE_SEPARATOR);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在切点之后织入
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
// @After("EleganceLog()")
|
||||
// public void doAfter() throws Throwable {
|
||||
// // 接口结束后换行,方便分割查看
|
||||
// logger.info("=========================================== End ===========================================" + LINE_SEPARATOR);
|
||||
// }
|
||||
// @AfterReturning("EleganceLog()")
|
||||
// public void afterRunning() throws Throwable {
|
||||
// // 接口结束后换行,方便分割查看
|
||||
// logger.info("=========================================== End ===========================================" + LINE_SEPARATOR);
|
||||
// }
|
||||
@AfterThrowing("EleganceLog()")
|
||||
public void afterThrowing() {
|
||||
// System.out.println("异常出现之后...afterThrowing");
|
||||
logger.info("=========================================== Exception Start===========================================" + LINE_SEPARATOR);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取切面注解的描述
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
* @return 描述信息
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getAspectLogDescription(JoinPoint joinPoint)
|
||||
throws Exception {
|
||||
String targetName = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
Object[] arguments = joinPoint.getArgs();
|
||||
Class targetClass = Class.forName(targetName);
|
||||
Method[] methods = targetClass.getMethods();
|
||||
StringBuilder description = new StringBuilder("");
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(methodName)) {
|
||||
Class[] clazzs = method.getParameterTypes();
|
||||
if (clazzs.length == arguments.length) {
|
||||
description.append(method.getAnnotation(EleganceLog.class).description());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return description.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.joju.datamanager.common.exception;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
public interface BaseErrorInfoInterface {
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getResultCode();
|
||||
|
||||
/**
|
||||
* 错误描述
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getResultMsg();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.joju.datamanager.common.exception;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
public enum ExceptionEnum implements BaseErrorInfoInterface {
|
||||
// 数据操作错误定义
|
||||
SUCCESS("2000", "成功!"),
|
||||
BODY_NOT_MATCH("4000", "请求的数据格式不符!"),
|
||||
SIGNATURE_NOT_MATCH("4001", "请求的数字签名不匹配!"),
|
||||
NOT_FOUND("4004", "未找到该资源!"),
|
||||
INTERNAL_SERVER_ERROR("5000", "服务器内部错误!"),
|
||||
SERVER_BUSY("5003", "服务器正忙,请稍后再试!");
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private final String resultCode;
|
||||
|
||||
/**
|
||||
* 错误描述
|
||||
*/
|
||||
private final String resultMsg;
|
||||
|
||||
ExceptionEnum(String resultCode, String resultMsg) {
|
||||
this.resultCode = resultCode;
|
||||
this.resultMsg = resultMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResultCode() {
|
||||
return resultCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResultMsg() {
|
||||
return resultMsg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.joju.datamanager.common.exception;
|
||||
|
||||
import com.joju.datamanager.common.result.Result;
|
||||
import com.joju.datamanager.common.result.ResultEnum;
|
||||
import com.joju.datamanager.common.result.ResultUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 处理自定义的业务异常
|
||||
*
|
||||
* @param req
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = SeMException.class)
|
||||
@ResponseBody
|
||||
public Result bizExceptionHandler(HttpServletRequest req, SeMException e) {
|
||||
logger.error("发生业务异常!原因是:{}", e.getErrorMsg());
|
||||
// return ResultUtil.error(e.getErrorCode(), e.getErrorMsg());
|
||||
return ResultUtil.failure(e.getErrorMsg(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理空指针的异常
|
||||
*
|
||||
* @param req
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = NullPointerException.class)
|
||||
@ResponseBody
|
||||
public Result exceptionHandler(HttpServletRequest req, NullPointerException e) {
|
||||
logger.error("发生空指针异常!原因是:", e);
|
||||
// return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);
|
||||
return ResultUtil.failure(ResultEnum.NULL_POINT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理其他异常
|
||||
*
|
||||
* @param req
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseBody
|
||||
public Result exceptionHandler(HttpServletRequest req, Exception e) {
|
||||
logger.error("未知异常!原因是:", e);
|
||||
return ResultUtil.failure(ResultEnum.SERVICE_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.joju.datamanager.common.exception;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/27
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class SeMException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
protected String errorCode;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
protected String errorMsg;
|
||||
|
||||
public SeMException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public SeMException(BaseErrorInfoInterface errorInfoInterface) {
|
||||
super(errorInfoInterface.getResultCode());
|
||||
this.errorCode = errorInfoInterface.getResultCode();
|
||||
this.errorMsg = errorInfoInterface.getResultMsg();
|
||||
}
|
||||
|
||||
public SeMException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
|
||||
super(errorInfoInterface.getResultCode(), cause);
|
||||
this.errorCode = errorInfoInterface.getResultCode();
|
||||
this.errorMsg = errorInfoInterface.getResultMsg();
|
||||
}
|
||||
|
||||
public SeMException(String errorMsg) {
|
||||
super(errorMsg);
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public SeMException(String errorCode, String errorMsg) {
|
||||
super(errorCode);
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public SeMException(String errorCode, String errorMsg, Throwable cause) {
|
||||
super(errorCode, cause);
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
|
||||
public String getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public void setErrorCode(String errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
public void setErrorMsg(String errorMsg) {
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
43
src/main/java/com/joju/datamanager/common/result/Result.java
Normal file
43
src/main/java/com/joju/datamanager/common/result/Result.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.joju.datamanager.common.result;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 描述:
|
||||
*
|
||||
* @author panxg
|
||||
* @date 2020年11月28日 11:56
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@ApiModel(value = "Result对象", description = "Result对象")
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "返回状态")
|
||||
private Integer status;
|
||||
|
||||
@ApiModelProperty(value = "返回状态码")
|
||||
private Integer code;
|
||||
|
||||
@ApiModelProperty(value = "返回描述")
|
||||
private String msg;
|
||||
|
||||
@ApiModelProperty(value = "返回数据")
|
||||
private T data;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.joju.datamanager.common.result;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
public class ResultConstant {
|
||||
|
||||
|
||||
public static final Integer RESULT_STATUS_SUCCESS = 1;
|
||||
public static final Integer RESULT_STATUS_FAILURE = -1;
|
||||
|
||||
public static final Integer RESULT_CODE_DEFAULT = 200;
|
||||
public static final Integer RESULT_CODE_FAILURE = -200;
|
||||
|
||||
public static final String RESULT_MSG_SUCCESS = "操作成功";
|
||||
public static final String RESULT_MSG_FAILURE = "操作失败";
|
||||
|
||||
}
|
||||
102
src/main/java/com/joju/datamanager/common/result/ResultEnum.java
Normal file
102
src/main/java/com/joju/datamanager/common/result/ResultEnum.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package com.joju.datamanager.common.result;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
public enum ResultEnum {
|
||||
|
||||
|
||||
LOGIN_OUT_SUCCESS(1, 200, "退出成功!"),
|
||||
LOGIN_IN_SUCCESS(1, 200, "登录成功!"),
|
||||
|
||||
AUTHORITY_FAILURE(-1, 200, "权限不足!"),
|
||||
|
||||
LOGIN_FAILURE_200401(-1, 200401, "登录失败!"),
|
||||
LOGIN_FAILURE_200402(-1, 200402, "登录信息已过期!"),
|
||||
LOGIN_FAILURE_200403(-1, 200403, "未登录,请先登录!"),
|
||||
|
||||
OPERATE_SUCCESS(1, 200, "操作成功!"),
|
||||
OPERATE_FAILURE(-1, 200, "操作失败!"),
|
||||
|
||||
QUERY_SUCCESS(1, 200, "查询成功!"),
|
||||
QUERY_FAILURE(-1, 200, "查询失败!"),
|
||||
|
||||
DELETE_SUCCESS(1, 200, "删除成功!"),
|
||||
DELETE_FAILURE(-1, 200, "删除失败!"),
|
||||
|
||||
INSERT_SUCCESS(1, 200, "新增成功!"),
|
||||
INSERT_FAILURE(-1, 200, "新增失败!"),
|
||||
|
||||
UPDATE_SUCCESS(1, 200, "修改成功!"),
|
||||
UPDATE_FAILURE(-1, 200, "修改失败!"),
|
||||
|
||||
SQL_FAILURE(-1, 200, "数据库异常!"),
|
||||
|
||||
HTTP_FAILURE(-1, 200, "请求异常!"),
|
||||
|
||||
SERVICE_FAILURE(-1, 200, "服务器异常!"),
|
||||
NULL_POINT(-1, 200, "服务器内部空指针!"),
|
||||
|
||||
PARAM_ERROR(-1,200,"请求参数有误"),
|
||||
|
||||
WX_PAY_EXCEPTION(-1,200,"微信异常"),
|
||||
;
|
||||
|
||||
|
||||
|
||||
private Integer status;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String msg;
|
||||
|
||||
ResultEnum(Integer status, Integer code, String msg) {
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
ResultEnum( Integer code, String msg) {
|
||||
this.status = 0;
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public ResultEnum setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ResultEnum setCode(Integer code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ResultEnum setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.joju.datamanager.common.result;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
|
||||
|
||||
import com.joju.datamanager.common.annotation.ResultAnnotation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* 描述:
|
||||
* 使用 @ControllerAdvice & ResponseBodyAdvice
|
||||
* 拦截Controller方法默认返回参数,统一处理返回值/响应体
|
||||
*/
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class ResultResponseBodyAdviceImpl implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Override
|
||||
public boolean supports(@NotNull MethodParameter returnType, @NotNull Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
|
||||
/*支持所有方法*/
|
||||
ResultAnnotation methodAnnotation = returnType.getMethodAnnotation(ResultAnnotation.class);
|
||||
if (methodAnnotation != null) {
|
||||
return methodAnnotation.required();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body,
|
||||
@NotNull MethodParameter returnType,
|
||||
@NotNull MediaType selectedContentType,
|
||||
@NotNull Class<? extends HttpMessageConverter<?>> selectedConverterType,
|
||||
@NotNull ServerHttpRequest request,
|
||||
@NotNull ServerHttpResponse response) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("ResultResponseBodyAdviceImpl.beforeBodyWrite:{}", body);
|
||||
}
|
||||
if (body instanceof Result) {
|
||||
return body;
|
||||
}
|
||||
return ResultUtil.successData(body);
|
||||
}
|
||||
}
|
||||
154
src/main/java/com/joju/datamanager/common/result/ResultUtil.java
Normal file
154
src/main/java/com/joju/datamanager/common/result/ResultUtil.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package com.joju.datamanager.common.result;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: Mr.zs
|
||||
* @date: 2023/12/25
|
||||
* @description:
|
||||
* @modifiedBy:
|
||||
* @version: 1.0
|
||||
*/
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
|
||||
|
||||
public class ResultUtil {
|
||||
|
||||
/**
|
||||
* @Description 成功
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:28
|
||||
**/
|
||||
public static <T> Result<T> success() {
|
||||
return ResultUtil.getResultSuccess();
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(ResultEnum resultEnum) {
|
||||
return ResultUtil.getResult(resultEnum);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(ResultEnum resultEnum,T data) {
|
||||
Result<T> result = ResultUtil.getResult(resultEnum);
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result<T> successMsg(String msg) {
|
||||
Result<T> resultSuccess = ResultUtil.getResultSuccess();
|
||||
resultSuccess.setMsg(msg);
|
||||
return resultSuccess;
|
||||
}
|
||||
|
||||
public static <T> Result<T> successData(T data) {
|
||||
Result<T> resultSuccess = ResultUtil.getResultSuccess();
|
||||
resultSuccess.setData(data);
|
||||
return resultSuccess;
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(String msg, T data) {
|
||||
Result<T> resultSuccess = ResultUtil.getResultSuccess();
|
||||
resultSuccess.setData(data);
|
||||
resultSuccess.setMsg(msg);
|
||||
return resultSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 失败
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:27
|
||||
**/
|
||||
public static <T> Result<T> failure() {
|
||||
return ResultUtil.getResultFailure();
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(ResultEnum resultEnum) {
|
||||
return ResultUtil.getResult(resultEnum);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(ResultEnum resultEnum,T data) {
|
||||
Result<T> result = ResultUtil.getResult(resultEnum);
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result<T> failureMsg(String msg) {
|
||||
return ResultUtil.getResultFailure(msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failureData(T data) {
|
||||
Result<T> resultFailure = ResultUtil.getResultFailure();
|
||||
resultFailure.setData(data);
|
||||
return resultFailure;
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(String msg, T data) {
|
||||
Result<T> resultFailure = ResultUtil.getResultFailure();
|
||||
resultFailure.setData(data);
|
||||
resultFailure.setMsg(msg);
|
||||
return resultFailure;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description 获取成功默认返回实体
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:27
|
||||
**/
|
||||
public static <T> Result<T> getResultSuccess() {
|
||||
Result<T> result = new Result<>();
|
||||
result.setStatus(ResultConstant.RESULT_STATUS_SUCCESS);
|
||||
result.setMsg(ResultConstant.RESULT_MSG_SUCCESS);
|
||||
result.setCode(ResultConstant.RESULT_CODE_DEFAULT);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 获取失败默认返回实体
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:27
|
||||
**/
|
||||
public static <T> Result<T> getResultFailure(String... msg) {
|
||||
Result<T> result = new Result<>();
|
||||
result.setStatus(ResultConstant.RESULT_STATUS_FAILURE);
|
||||
if (ArrayUtil.isEmpty(msg)) {
|
||||
result.setMsg(ResultConstant.RESULT_MSG_FAILURE);
|
||||
}else{
|
||||
result.setMsg(msg[0]);
|
||||
}
|
||||
result.setCode(ResultConstant.RESULT_CODE_DEFAULT);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 获取默认返回实体
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:27
|
||||
**/
|
||||
public static <T> Result<T> getResult() {
|
||||
Result<T> result = new Result<>();
|
||||
result.setStatus(ResultConstant.RESULT_STATUS_SUCCESS);
|
||||
result.setMsg(ResultConstant.RESULT_MSG_SUCCESS);
|
||||
result.setCode(ResultConstant.RESULT_CODE_DEFAULT);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 获取枚举入参返回实体
|
||||
* @Param @return @exception
|
||||
* @Author panxg
|
||||
* @Date 2020/11/28 12:27
|
||||
**/
|
||||
public static <T> Result<T> getResult(ResultEnum resultEnum) {
|
||||
Result<T> result = new Result<>();
|
||||
result.setStatus(resultEnum.getStatus());
|
||||
result.setMsg(resultEnum.getMsg());
|
||||
result.setCode(resultEnum.getCode());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user