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

onMessage 要转换的消息类型为 byte[] 时,报错:ClassCastException: java.lang.String cannot be cast to [B #695

Open
BrightX opened this issue Dec 3, 2024 · 0 comments

Comments

@BrightX
Copy link

BrightX commented Dec 3, 2024

String str = new String(messageExt.getBody(), Charset.forName(charset));
if (Objects.equals(messageType, String.class)) {
return str;
} else {
// If msgType not string, use objectMapper change it.
try {
if (messageType instanceof Class) {
//if the messageType has not Generic Parameter
return this.getMessageConverter().fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) messageType);

image

我注意到这里当要转换的类型为 byte[] 时,会调用spring-messagingByteArrayMessageConverterconvertFromInternal,而这个方法会原样返回 payload。而截图的 L551,将 payload 设置成了 str,导致 Stringbyte[] 的类型转换异常 ClassCastException: java.lang.String cannot be cast to [B

org.springframework.messaging.converter.ByteArrayMessageConverter#convertFromInternal

    return message.getPayload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant