add ObjectMapper arg for RedisSerializers.jacksonSerializer
This commit is contained in:
@@ -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