This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
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 数据体
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2023-08-09 10:36
|
||||
* @deprecated 0.0.3 过时,0.0.5 移除,单参数建议 url 传参
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Deprecated
|
||||
public @interface RequestSingleParam {
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.imyeyu.spring.annotation;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import jakarta.annotation.Nonnull;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import org.springframework.core.MethodParameter;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 单参数请求解析器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-10-13 16:29
|
||||
* @deprecated 0.0.3 过时,0.0.5 移除,单参数建议 url 传参
|
||||
*/
|
||||
@Component
|
||||
@Deprecated
|
||||
public class RequestSingleParamResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
/**
|
||||
* 创建单参数解析器
|
||||
*/
|
||||
public RequestSingleParamResolver() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.hasParameterAnnotation(RequestSingleParam.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(@Nonnull MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
if (request == null) {
|
||||
throw new TimiException(TimiCode.REQUEST_BAD, "request illegal");
|
||||
}
|
||||
JsonElement element = JsonParser.parseString(IO.toString(request.getInputStream()));
|
||||
if (!element.isJsonObject()) {
|
||||
throw new TimiException(TimiCode.ARG_BAD, "not json object");
|
||||
}
|
||||
JsonObject object = element.getAsJsonObject();
|
||||
String parameterName = parameter.getParameterName();
|
||||
if (!object.has(parameterName)) {
|
||||
throw new TimiException(TimiCode.ARG_MISS, "not found " + parameterName + " param");
|
||||
}
|
||||
JsonElement el = object.get(parameterName);
|
||||
if (parameter.getParameterType().isAssignableFrom(Long.class)) {
|
||||
return el.getAsLong();
|
||||
}
|
||||
if (parameter.getParameterType().isAssignableFrom(Integer.class)) {
|
||||
return el.getAsInt();
|
||||
}
|
||||
if (parameter.getParameterType().isAssignableFrom(String.class)) {
|
||||
return el.getAsString();
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR, "not support parameter type");
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.imyeyu.spring.handler;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* MySQL JSON 数据类型处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-07-04 09:36
|
||||
*/
|
||||
public class GsonHandler extends BaseTypeHandler<JsonElement> {
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, JsonElement parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setString(i, parameter.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
return toElement(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return toElement(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return toElement(cs.getNString(columnIndex));
|
||||
}
|
||||
|
||||
private JsonElement toElement(String json) {
|
||||
if (TimiJava.isNotEmpty(json)) {
|
||||
JsonElement el = JsonParser.parseString(json);
|
||||
if (el.isJsonObject()) {
|
||||
return el.getAsJsonObject();
|
||||
}
|
||||
if (el.isJsonArray()) {
|
||||
return el.getAsJsonArray();
|
||||
}
|
||||
if (el.isJsonPrimitive()) {
|
||||
return el.getAsJsonPrimitive();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.imyeyu.spring.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-03-16 15:42
|
||||
*/
|
||||
@MappedTypes(JsonNode.class)
|
||||
@MappedJdbcTypes({JdbcType.VARCHAR, JdbcType.CLOB, JdbcType.LONGVARCHAR})
|
||||
public class JsonNodeTypeHandler extends BaseTypeHandler<JsonNode> {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, JsonNode parameter, JdbcType jdbcType) throws SQLException {
|
||||
try {
|
||||
ps.setString(i, MAPPER.writeValueAsString(parameter));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Failed to serialize JsonNode to JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
return parseJson(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return parseJson(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonNode getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return parseJson(cs.getString(columnIndex));
|
||||
}
|
||||
|
||||
private JsonNode parseJson(String json) throws SQLException {
|
||||
if (json == null || json.isEmpty()) {
|
||||
return MAPPER.createObjectNode();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SQLException("Failed to parse JSON to JsonNode", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
@@ -83,20 +85,24 @@ public class RedisSerializers {
|
||||
};
|
||||
|
||||
/**
|
||||
* Gson 序列化
|
||||
* Json 序列化
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
* @param clazz 数据类型
|
||||
* @return Redis 序列化器
|
||||
*/
|
||||
public static <T> RedisSerializer<T> gsonSerializer(Class<T> clazz) {
|
||||
public static <T> RedisSerializer<T> jacksonSerializer(Class<T> clazz) {
|
||||
return new RedisSerializer<>() {
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
private static final ObjectMapper JACKSON = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T object) throws SerializationException {
|
||||
return GSON.toJson(object).getBytes(StandardCharsets.UTF_8);
|
||||
try {
|
||||
return JACKSON.writeValueAsString(object).getBytes(StandardCharsets.UTF_8);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +110,11 @@ public class RedisSerializers {
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
return GSON.fromJson(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||
try {
|
||||
return JACKSON.readValue(new String(bytes, StandardCharsets.UTF_8), clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user