Compare commits
15 Commits
62de8e4885
...
v0.0.9
| Author | SHA1 | Date | |
|---|---|---|---|
| f5528fbfad | |||
|
|
31bb990d82 | ||
|
|
8a6e148d6c | ||
|
|
b94849d69c | ||
|
|
3f97bb1356 | ||
| 90c97f211b | |||
|
|
8d89c60a02 | ||
| 42dee2e4ad | |||
|
|
d77cf10b1b | ||
| 66eb6108cd | |||
|
|
3a343724a8 | ||
| 82f950e71d | |||
|
|
83b1265c0a | ||
| cceabb0c8d | |||
|
|
b16656af12 |
24
pom.xml
24
pom.xml
@@ -7,22 +7,22 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.5.11</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>com.imyeyu.spring</groupId>
|
||||
<artifactId>timi-spring</artifactId>
|
||||
<version>0.0.4</version>
|
||||
<version>0.0.9</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<springboot.version>3.4.0</springboot.version>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
<springboot.version>3.5.11</springboot.version>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
@@ -102,20 +102,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-parameter-names</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -155,7 +141,7 @@
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.io</groupId>
|
||||
<artifactId>timi-io</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<version>0.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.imyeyu.spring.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 读取 JSON 请求体中的单个字段并绑定到接口参数。
|
||||
*
|
||||
* <p>默认使用方法参数名作为 JSON 字段名,也可以手动指定字段名。
|
||||
*
|
||||
* <pre>
|
||||
* public void run(@RequestBodyValue String data) {
|
||||
* }
|
||||
*
|
||||
* public void run(@RequestBodyValue("value") Long id) {
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2026-04-08 11:00
|
||||
*/
|
||||
@Documented
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequestBodyValue {
|
||||
|
||||
/**
|
||||
* JSON 字段名,为空时使用方法参数名。
|
||||
*
|
||||
* @return JSON 字段名
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 是否必须存在该字段。
|
||||
*
|
||||
* @return true 表示必须存在
|
||||
*/
|
||||
boolean required() default true;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.imyeyu.spring.annotation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
/**
|
||||
* {@link RequestBodyValue} 参数解析器。
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2026-04-08 11:00
|
||||
*/
|
||||
@Component
|
||||
public class RequestBodyValueArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private static final String REQUEST_BODY_JSON_NODE_ATTR = RequestBodyValueArgumentResolver.class.getName() + ".REQUEST_BODY_JSON_NODE";
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 创建 {@link RequestBodyValue} 参数解析器。
|
||||
*
|
||||
* @param objectMapper Jackson 对象映射器
|
||||
*/
|
||||
public RequestBodyValueArgumentResolver(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(@NonNull MethodParameter parameter) {
|
||||
return parameter.hasParameterAnnotation(RequestBodyValue.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(@NonNull MethodParameter parameter, ModelAndViewContainer mavContainer, @NonNull NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
RequestBodyValue bodyValue = parameter.getParameterAnnotation(RequestBodyValue.class);
|
||||
if (bodyValue == null) {
|
||||
return null;
|
||||
}
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
TimiException.required(request, "not found request");
|
||||
assert request != null;
|
||||
|
||||
JsonNode requestBody;
|
||||
{
|
||||
Object cached = request.getAttribute(REQUEST_BODY_JSON_NODE_ATTR);
|
||||
if (cached instanceof JsonNode jsonNode) {
|
||||
return jsonNode;
|
||||
}
|
||||
byte[] bodyBytes = request.getInputStream().readAllBytes();
|
||||
TimiException.requiredTrue(1 < bodyBytes.length, "empty request body");
|
||||
requestBody = objectMapper.readTree(bodyBytes);
|
||||
TimiException.requiredTrue(requestBody != null && requestBody.isObject(), "not object request body");
|
||||
request.setAttribute(REQUEST_BODY_JSON_NODE_ATTR, requestBody);
|
||||
}
|
||||
String fieldName = bodyValue.value();
|
||||
{
|
||||
if (TimiJava.isEmpty(fieldName)) {
|
||||
fieldName = parameter.getParameterName();
|
||||
}
|
||||
TimiException.required(fieldName, "not found @RequestBodyValue parameter name");
|
||||
}
|
||||
JsonNode fieldNode = requestBody.get(fieldName);
|
||||
if (fieldNode == null || fieldNode.isMissingNode() || fieldNode.isNull()) {
|
||||
if (!bodyValue.required()) {
|
||||
return null;
|
||||
}
|
||||
throw new TimiException(TimiCode.ARG_MISS, "not found json field: %s".formatted(fieldName));
|
||||
}
|
||||
return objectMapper.treeToValue(fieldNode, parameter.getParameterType());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.imyeyu.spring.config;
|
||||
|
||||
import com.imyeyu.spring.bean.RedisConfigParams;
|
||||
import com.imyeyu.spring.util.Redis;
|
||||
import io.lettuce.core.api.StatefulConnection;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.cache.annotation.CachingConfigurer;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
@@ -39,7 +40,7 @@ public abstract class AbstractRedisConfig implements CachingConfigurer {
|
||||
* 连接池配置
|
||||
* <p>参考:
|
||||
* <pre>
|
||||
* GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
|
||||
* GenericObjectPoolConfig<StatefulConnection<?, ?>> config = new GenericObjectPoolConfig<>();
|
||||
* config.setMaxTotal(config.getMaxActive());
|
||||
* config.setMinIdle(config.getMinIdle());
|
||||
* config.setMaxIdle(config.getMaxIdle());
|
||||
@@ -48,7 +49,7 @@ public abstract class AbstractRedisConfig implements CachingConfigurer {
|
||||
*
|
||||
* @return GenericObjectPoolConfig
|
||||
*/
|
||||
public abstract GenericObjectPoolConfig<?> getPoolConfig();
|
||||
public abstract GenericObjectPoolConfig<StatefulConnection<?, ?>> getPoolConfig();
|
||||
|
||||
/**
|
||||
* Redis key 生成策略
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package com.imyeyu.spring.bean;
|
||||
package com.imyeyu.spring.entity;
|
||||
|
||||
import com.imyeyu.java.bean.Language;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.spring.annotation.table.AutoUUID;
|
||||
import com.imyeyu.spring.annotation.table.Id;
|
||||
import com.imyeyu.spring.entity.Creatable;
|
||||
import com.imyeyu.spring.entity.Deletable;
|
||||
import com.imyeyu.spring.entity.IDEntity;
|
||||
import com.imyeyu.spring.entity.Updatable;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -19,7 +15,7 @@ import lombok.EqualsAndHashCode;
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Multilingual extends Language implements IDEntity<String>, Creatable, Updatable, Deletable {
|
||||
public class Multilingual extends Language implements IDEntity<String>, Creatable, Updatable, Deletable, Destroyable {
|
||||
|
||||
/** 唯一标识 */
|
||||
@Id
|
||||
@@ -3,6 +3,9 @@ package com.imyeyu.spring.util;
|
||||
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;
|
||||
@@ -27,7 +30,13 @@ import java.util.function.Consumer;
|
||||
* @author 夜雨
|
||||
* @version 2021-11-21 09:58
|
||||
*/
|
||||
public record Redis<K, V>(RedisTemplate<K, V> redis, RedisSerializer<K> serializer) {
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Redis<K, V> {
|
||||
|
||||
private RedisTemplate<K, V> redis;
|
||||
private RedisSerializer<K> serializer;
|
||||
|
||||
/**
|
||||
* 加锁
|
||||
|
||||
@@ -87,19 +87,29 @@ public class RedisSerializers {
|
||||
/**
|
||||
* Json 序列化
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
* @param <T> 数据类型
|
||||
* @param clazz 数据类型
|
||||
* @return Redis 序列化器
|
||||
*/
|
||||
public static <T> RedisSerializer<T> jacksonSerializer(Class<T> clazz) {
|
||||
return new RedisSerializer<>() {
|
||||
return jacksonSerializer(new ObjectMapper(), clazz);
|
||||
}
|
||||
|
||||
private static final ObjectMapper JACKSON = new ObjectMapper();
|
||||
/**
|
||||
* Json 序列化
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
* @param mapper 序列化对象
|
||||
* @param clazz 数据类型
|
||||
* @return Redis 序列化器
|
||||
*/
|
||||
public static <T> RedisSerializer<T> jacksonSerializer(ObjectMapper mapper, Class<T> clazz) {
|
||||
return new RedisSerializer<>() {
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T object) throws SerializationException {
|
||||
try {
|
||||
return JACKSON.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
||||
return mapper.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -111,7 +121,7 @@ public class RedisSerializers {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return JACKSON.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||
return mapper.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user