Compare commits
6
Commits
45124eba81
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9c7c098c8 | ||
|
|
a4a9317731 | ||
|
|
cf701b18fa | ||
|
|
f42b833e3e | ||
|
|
ebaeb1ac56 | ||
|
|
7e1ff1343d |
@@ -13,7 +13,7 @@
|
||||
|
||||
<groupId>com.imyeyu.spring</groupId>
|
||||
<artifactId>timi-spring</artifactId>
|
||||
<version>0.0.16</version>
|
||||
<version>0.0.19</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -143,6 +143,16 @@
|
||||
<artifactId>timi-io</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.java</groupId>
|
||||
<artifactId>timi-java</artifactId>
|
||||
<version>0.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.lang</groupId>
|
||||
<artifactId>timi-lang</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.lang.annotation.Target;
|
||||
/**
|
||||
* 应用在 Controller 的接口上
|
||||
*
|
||||
* <p>{@link #lifeCycle()} 生命周期内(秒)限制访问 {@link #value()} 次
|
||||
* <p>{@link #inSeconds()} 生命周期内(秒)限制访问 {@link #value()} 次
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2021-08-16 17:57
|
||||
@@ -32,5 +32,5 @@ public @interface RequestRateLimit {
|
||||
*
|
||||
* @return 生命周期秒数
|
||||
*/
|
||||
int lifeCycle() default 10;
|
||||
int inSeconds() default 10;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public abstract class RequestRateLimitAbstractInterceptor implements HandlerInte
|
||||
return true;
|
||||
}
|
||||
// 频率限制
|
||||
return beforeRun(req, resp, buildId(handlerMethod), requestRateLimit.lifeCycle(), requestRateLimit.value());
|
||||
return beforeRun(req, resp, buildId(handlerMethod), requestRateLimit.inSeconds(), requestRateLimit.value());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -37,34 +37,6 @@ public class Page<T> extends BasePage {
|
||||
/** 排序字段映射 */
|
||||
protected LinkedHashMap<String, BaseMapper.OrderType> orderMap;
|
||||
|
||||
/**
|
||||
* 创建分页参数
|
||||
*
|
||||
* @param index 页码
|
||||
* @param size 每页数量
|
||||
*/
|
||||
public Page(int index, int size) {
|
||||
super(index, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取偏移量
|
||||
*
|
||||
* @return 偏移量
|
||||
*/
|
||||
public long getOffset() {
|
||||
return (long) index * size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取限制数量
|
||||
*
|
||||
* @return 限制数量
|
||||
*/
|
||||
public long getLimit() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加排序字段
|
||||
*
|
||||
|
||||
@@ -231,7 +231,7 @@ public abstract class BaseSQLProvider {
|
||||
Ref.setFieldValue(entity, fc.field, uuid);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new TimiException(TimiCode.ERROR).msgKey("auto set field:%s value error".formatted(fc.fieldName));
|
||||
throw new TimiException(TimiCode.ERROR, "auto set field:%s value error".formatted(fc.fieldName));
|
||||
}
|
||||
if (entity instanceof Creatable creatableEntity && creatableEntity.getCreatedAt() == null) {
|
||||
creatableEntity.setCreatedAt(Time.now());
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiError;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.lang.message.MessageResolver;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -11,6 +14,7 @@ import jakarta.validation.ValidationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.converter.HttpMessageConversionException;
|
||||
import org.springframework.validation.BindException;
|
||||
@@ -30,10 +34,15 @@ public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
private final ObjectProvider<MessageResolver> messageResolverProvider;
|
||||
|
||||
/**
|
||||
* 创建全局异常处理器
|
||||
*
|
||||
* @param messageResolverProvider 消息解析器提供器
|
||||
*/
|
||||
public GlobalExceptionHandler() {
|
||||
public GlobalExceptionHandler(ObjectProvider<MessageResolver> messageResolverProvider) {
|
||||
this.messageResolverProvider = messageResolverProvider;
|
||||
}
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
@@ -55,7 +64,7 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD).msgKey("invalid.body");
|
||||
return fail(TimiCode.ARG_BAD, "invalid.body");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +83,7 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.request");
|
||||
return fail(TimiCode.REQUEST_BAD, "invalid.request");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,19 +92,20 @@ public class GlobalExceptionHandler {
|
||||
* @param e 异常
|
||||
* @return 异常返回
|
||||
*/
|
||||
@IgnoreGlobalReturn
|
||||
@ExceptionHandler({BindException.class, ValidationException.class, MethodArgumentNotValidException.class, TypeMismatchException.class})
|
||||
public TimiResponse<?> paramsException(Exception e) {
|
||||
if (e instanceof MethodArgumentNotValidException subE) {
|
||||
log.warn("request error", e);
|
||||
FieldError error = subE.getBindingResult().getFieldError();
|
||||
if (error != null) {
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD, "[%s] %s".formatted(error.getField(), error.getDefaultMessage()));
|
||||
return fail(TimiCode.ARG_BAD, "[%s] %s".formatted(error.getField(), error.getDefaultMessage()));
|
||||
}
|
||||
}
|
||||
if (env.startsWith("dev") || log.isDebugEnabled()) {
|
||||
log.error("request error", e);
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.arg");
|
||||
return fail(TimiCode.REQUEST_BAD, "invalid.arg");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,15 +124,81 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
// 一般异常
|
||||
return timiE.toResponse();
|
||||
return fail(timiE);
|
||||
}
|
||||
if (e instanceof TimiError timiE) {
|
||||
log.error(timiE.getMessage(), e);
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return fail(timiE.getCode(), timiE.getMessageCode(), timiE.getMessageArgs());
|
||||
}
|
||||
// 致命异常
|
||||
log.error("fatal error", e);
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.ERROR).msgKey("service.error");
|
||||
return fail(TimiCode.ERROR, "service.error");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param e 异常
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiException e) {
|
||||
return fail(e.getCode(), e.getMessageCode(), e.getMessageArgs());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiCode code, String messageCode) {
|
||||
return fail(code, messageCode, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiCode code, String messageCode, java.util.Map<String, Object> messageArgs) {
|
||||
TimiResponse<?> result = new TimiResponse<>(code).messageCode(messageCode, messageArgs);
|
||||
result.setMessage(resolveMessage(code, messageCode, messageArgs));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析消息
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
* @return 消息
|
||||
*/
|
||||
private String resolveMessage(TimiCode code, String messageCode, java.util.Map<String, Object> messageArgs) {
|
||||
if (TimiJava.isEmpty(messageCode)) {
|
||||
return code.toString();
|
||||
}
|
||||
try {
|
||||
MessageResolver messageResolver = messageResolverProvider.getIfAvailable();
|
||||
if (messageResolver == null) {
|
||||
return messageCode;
|
||||
}
|
||||
String message = messageResolver.resolve(messageCode, messageArgs, TimiSpring.getLanguage());
|
||||
return TimiJava.isEmpty(message) ? messageCode : message;
|
||||
} catch (Exception e) {
|
||||
log.error("message resolve error", e);
|
||||
return messageCode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import com.imyeyu.java.bean.LanguageMsgMapping;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.AOPLogInterceptor;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import lombok.Data;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -28,15 +24,11 @@ import java.util.Objects;
|
||||
* @author 夜雨
|
||||
* @version 2023-04-30 00:59
|
||||
*/
|
||||
@Data
|
||||
@RestControllerAdvice
|
||||
public class GlobalReturnHandler implements ResponseBodyAdvice<Object> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalReturnHandler.class);
|
||||
|
||||
/** 多语言头处理回调 */
|
||||
private CallbackArgReturn<LanguageMsgMapping<?>, String> multilingualHeader;
|
||||
|
||||
/**
|
||||
* 创建全局返回处理器
|
||||
*/
|
||||
@@ -59,23 +51,12 @@ public class GlobalReturnHandler implements ResponseBodyAdvice<Object> {
|
||||
{
|
||||
TimiResponse<?> result;
|
||||
if (body instanceof TimiResponse<?> timiResponse) {
|
||||
// 可能已被全局异常包装
|
||||
result = timiResponse;
|
||||
} else {
|
||||
result = new TimiResponse<>(TimiCode.SUCCESS, body);
|
||||
}
|
||||
try {
|
||||
if (multilingualHeader != null && TimiJava.isNotEmpty(result.getMsgKey())) {
|
||||
result.setMsg(multilingualHeader.handler(result));
|
||||
} else if (TimiJava.isEmpty(result.getMsg())) {
|
||||
result.setMsg(TimiCode.fromCode(result.getCode()).toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("multilingual response error", e);
|
||||
result.setMsg(TimiCode.fromCode(result.getCode()).toString());
|
||||
}
|
||||
if (30000 < result.getCode()) {
|
||||
log.warn("ID: {} Response -> Exception.{}.{}", TimiSpring.getSessionAttr(AOPLogInterceptor.REQUEST_ID), result.getCode(), result.getMsg());
|
||||
log.warn("ID: {} Response -> Exception.{}.{}", TimiSpring.getSessionAttr(AOPLogInterceptor.REQUEST_ID), result.getCode(), result.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.spring.config.AbstractRedisConfig;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -30,8 +28,6 @@ import java.util.function.Consumer;
|
||||
* @author 夜雨
|
||||
* @version 2021-11-21 09:58
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Redis<K, V> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user