Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]给redis做序列化,入参为btye数组,报错ArrayIndexOutOfBoundsException #537

Closed
ALLCX opened this issue Jul 8, 2022 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@ALLCX
Copy link

ALLCX commented Jul 8, 2022

版本

Springboot: 2.1.3.RELEASE
JAVA: OpenJDK 1.8
fastjson: 2.0.8

问题描述

要存入redis的是一个byte数组

redisTemplate配置有三种情况:
一:使用fastjson1

template.setValueSerializer(new GenericFastJsonRedisSerializer());
  1. 序列化: value会被序列化为16进制的字符串存入redis中;
  2. 反序列化: 获取时反序列化后直接得到一个byte数组。

二:使用fastjson2 JSON

template.setValueSerializer(new GenericFastJsonRedisSerializer());
  1. 序列化:value会直接作为字符串存入redis中;
  2. 反序列化:获取时反序列化会得到一个JSONArray,需要做额外的判断的处理....这个怎么配置可以直接得到一个btye数组呢?

三:使用fastjson2 JSONB

template.setValueSerializer(new GenericFastJsonRedisSerializer(true));
  1. 序列化:报错 ArrayIndexOutOfBoundsException

相关日志输出

在JSONWriterJSONB类的public void writeBinary(byte[] bytes)方法中

void ensureCapacity(int minCapacity)
// 这个方法给 this.bytes调整长度,得到的长度比需要序列化的byte数组bytes长了10位,但是在下面的方法中
System.arraycopy(bytes, 0, this.bytes, off, bytes.length);
//off 是11,这样正好把bytes复制到this.bytes时正好少了一位
//导致报错ArrayIndexOutOfBoundsException

redis报错

@ALLCX ALLCX added the bug Something isn't working label Jul 8, 2022
@wenshao wenshao added this to the 2.0.9 milestone Jul 8, 2022
@wenshao
Copy link
Member

wenshao commented Jul 8, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.9-SNAPSHOT/
问题已经修复,请用2.0.9-SNAPSHOT版本验证

@wenshao
Copy link
Member

wenshao commented Jul 10, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.9
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants