java bootstrap 开发语言 JsonParseException: Unexpected character (‘a‘ (code 97)): Expected space separating root-level value - readAllEntrySet()
1. 异常场景
项目初始集成redis使用的是默认的lettuce客户端,后来因为业务需要切换成redisson客户端 使用redisson客户端后获取原先存入redis中的值出现了报错: JsonParseException: Unexpected character (‘a’ (code 97)): Expected space separating root-level value
2. 解决方案
2.1 设置编解码格式StringCodec
获取所有数据的时候,无引号字符串无法通过JsonJacksonCodec进行解析导致后续报错,所以可以通过设置Codec的方式来进行处理
RMapmap = redissonClient.getMap("key", StringCodec.INSTANCE);
Set> entries = map.readAllEntrySet();缺点:如果原始数据中value存储的是对象,通过设置StringCodec取出的值也会是字符串格式,需要额外进行序列化操作。
2.1 自定义编码格式
默认JacksonCodec编码初始化包含有对象转换设置
protected void init(ObjectMapper objectMapper) {
objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(Visibility.ANY).withGetterVisibility(Visibility.NONE).withSetterVisibility(Visibility.NONE).withCreatorVisibility(Visibility.NONE));
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.enable(new JsonGenerator.Feature[]{Feature.WRITE_BIGDECIMAL_AS_PLAIN});
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.enable(new MapperFeature[]{MapperFeature.SORT_PROPERTIES_ALPHABETICALLY});
}
自定义Codec
public class RedissonCodec extends BaseCodec {
protected final ObjectMapper mapObjectMapper;
/** 拼多多帮助力入口
* @see
*/
private final Encoder encoder = new Encoder() {
@Override
public ByteBuf encode(Object in) throws IOException {
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
try {
ByteBufOutputStream os = new ByteBufOutputStream(out);
mapObjectMapper.writeValue((OutputStream) os, in);
return os.buffer();
} catch (IOException e) {
out.release();
throw e;
} catch (Exception e) {
out.release();
throw new IOException(e);
}
}
};
private final Decoder