Compare commits
15 Commits
v0.0.4
...
f5528fbfad
| 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>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>3.4.0</version>
|
<version>3.5.11</version>
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.imyeyu.spring</groupId>
|
<groupId>com.imyeyu.spring</groupId>
|
||||||
<artifactId>timi-spring</artifactId>
|
<artifactId>timi-spring</artifactId>
|
||||||
<version>0.0.4</version>
|
<version>0.0.9</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>21</java.version>
|
<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.source>21</maven.compiler.source>
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.test.skip>true</maven.test.skip>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -102,20 +102,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<version>${springboot.version}</version>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@@ -155,7 +141,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.imyeyu.io</groupId>
|
<groupId>com.imyeyu.io</groupId>
|
||||||
<artifactId>timi-io</artifactId>
|
<artifactId>timi-io</artifactId>
|
||||||
<version>0.0.2</version>
|
<version>0.0.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<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.bean.RedisConfigParams;
|
||||||
import com.imyeyu.spring.util.Redis;
|
import com.imyeyu.spring.util.Redis;
|
||||||
|
import io.lettuce.core.api.StatefulConnection;
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
import org.springframework.cache.annotation.CachingConfigurer;
|
import org.springframework.cache.annotation.CachingConfigurer;
|
||||||
import org.springframework.cache.interceptor.KeyGenerator;
|
import org.springframework.cache.interceptor.KeyGenerator;
|
||||||
@@ -39,7 +40,7 @@ public abstract class AbstractRedisConfig implements CachingConfigurer {
|
|||||||
* 连接池配置
|
* 连接池配置
|
||||||
* <p>参考:
|
* <p>参考:
|
||||||
* <pre>
|
* <pre>
|
||||||
* GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
|
* GenericObjectPoolConfig<StatefulConnection<?, ?>> config = new GenericObjectPoolConfig<>();
|
||||||
* config.setMaxTotal(config.getMaxActive());
|
* config.setMaxTotal(config.getMaxActive());
|
||||||
* config.setMinIdle(config.getMinIdle());
|
* config.setMinIdle(config.getMinIdle());
|
||||||
* config.setMaxIdle(config.getMaxIdle());
|
* config.setMaxIdle(config.getMaxIdle());
|
||||||
@@ -48,7 +49,7 @@ public abstract class AbstractRedisConfig implements CachingConfigurer {
|
|||||||
*
|
*
|
||||||
* @return GenericObjectPoolConfig
|
* @return GenericObjectPoolConfig
|
||||||
*/
|
*/
|
||||||
public abstract GenericObjectPoolConfig<?> getPoolConfig();
|
public abstract GenericObjectPoolConfig<StatefulConnection<?, ?>> getPoolConfig();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis key 生成策略
|
* 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.bean.Language;
|
||||||
import com.imyeyu.java.ref.Ref;
|
import com.imyeyu.java.ref.Ref;
|
||||||
import com.imyeyu.spring.annotation.table.AutoUUID;
|
import com.imyeyu.spring.annotation.table.AutoUUID;
|
||||||
import com.imyeyu.spring.annotation.table.Id;
|
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.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
@@ -19,7 +15,7 @@ import lombok.EqualsAndHashCode;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@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
|
@Id
|
||||||
@@ -3,6 +3,9 @@ package com.imyeyu.spring.util;
|
|||||||
import com.imyeyu.java.TimiJava;
|
import com.imyeyu.java.TimiJava;
|
||||||
import com.imyeyu.java.bean.timi.TimiException;
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
import com.imyeyu.spring.config.AbstractRedisConfig;
|
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.connection.RedisConnection;
|
||||||
import org.springframework.data.redis.core.Cursor;
|
import org.springframework.data.redis.core.Cursor;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@@ -27,7 +30,13 @@ import java.util.function.Consumer;
|
|||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
* @version 2021-11-21 09:58
|
* @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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加锁
|
* 加锁
|
||||||
|
|||||||
@@ -92,14 +92,24 @@ public class RedisSerializers {
|
|||||||
* @return Redis 序列化器
|
* @return Redis 序列化器
|
||||||
*/
|
*/
|
||||||
public static <T> RedisSerializer<T> jacksonSerializer(Class<T> clazz) {
|
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
|
@Override
|
||||||
public byte[] serialize(T object) throws SerializationException {
|
public byte[] serialize(T object) throws SerializationException {
|
||||||
try {
|
try {
|
||||||
return JACKSON.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
return mapper.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -111,7 +121,7 @@ public class RedisSerializers {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return JACKSON.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
return mapper.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user