[BUG]redis配置fastjson2序列化方式,用pipeline方式获取key的时候报错
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*简要描述您碰到的问题。*
redis配置fastjson2序列化和反序列化,但是用spring自带的restTemplate中的executepipeline方式获取key的时候会报错:
com.alibaba.fastjson2.JSONException: offset 1, character i, line 1, column 2, fastjson-version 2.0.37 inv:100002:100020
### 重现步骤
代码示例:
```java
public List getKeysWithPipeline(List keyPatterns) {
return redisTemplate.executePipelined((RedisCallback) connection -> {
for (String keyPattern : keyPatterns) {
connection.keys(keyPattern.getBytes());
}
return null;
});
}
// 调用方式
List keysWithPipeline = redisTemplateService.getKeysWithPipeline(Arrays.asList("inv:100002:*"));
```
### 相关日志输出

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
做了redis的序列化和反序列化的配置
```java
public class FastJson2JsonRedisSerializer implements RedisSerializer {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class clazz;
public FastJson2JsonRedisSerializer(Class clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);
}
}
public class RedisConfig extends CachingConfigurerSupport {
@Bean
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
```
Contributor guide
Research direction
Start with RedisTemplate.executePipelined and the FastJson2JsonRedisSerializer and RedisConfig examples in the report. Reproduce the keys call using the shown pipeline code and inspect where JSON.parseObject receives the Redis response. Done means the pipeline retrieves the requested keys without the reported fastjson2 exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, redis
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100