Closed
Description
Affects: org.springframework:spring-messaging:5.3.6
We are using org.springframework:spring-messaging:5.3.6 with com.google.protobuf:protobuf-java:3.13.0 and com.google.protobuf:protobuf-java-util:3.13.0 to send and receive protobuf messages as application/json.
static class ProtobufJavaUtilSupport implements ProtobufFormatSupport {
...
@Override
public void merge(org.springframework.messaging.Message<?> message, Charset charset,
MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)
throws IOException, MessageConversionException {
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
this.parser.merge(message.getPayload().toString(), builder);
}
else {
throw new MessageConversionException(
"protobuf-java-util does not support parsing " + contentType);
}
}
...
}
message.getPayload()
returns a byte array (GenericMessage [payload=byte[230], headers={...}]). Calling toString
on a byte array produces a string similar to "[B@27fe3806".
Attempt to parse resulting string as JSON produces the following error:
com.google.protobuf.InvalidProtocolBufferException: java.io.EOFException: End of input at line 1 column 12 path $[1]
at com.google.protobuf.util.JsonFormat$ParserImpl.merge (JsonFormat.java:1347)
at com.google.protobuf.util.JsonFormat$Parser.merge (JsonFormat.java:477)
at org.springframework.messaging.converter.ProtobufMessageConverter$ProtobufJavaUtilSupport.merge (ProtobufMessageConverter.java:265)
at org.springframework.messaging.converter.ProtobufMessageConverter.convertFromInternal (ProtobufMessageConverter.java:150)
I assume that byte arrays need special handling so that we use String constructor instead of toString()
call in order to get actual JSON content (i.e. something akin to new String(message.getPayload(), StandardCharsets.UTF_8)
).
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
snicoll commentedon Sep 26, 2023
Yes although it looks a bit odd to me that the
MimeType
of the body isapplication/json
and then the actual content of the body is not text. To make sure I am not missing anything, can you please share a small sample that we can run ourselves? You can do so by attaching a zip to this issue or pushing the code to a separate GitHub repository.spring-projects-issues commentedon Oct 3, 2023
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
spring-projects-issues commentedon Oct 10, 2023
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
gnagy commentedon Apr 17, 2024
Hi, I just ran into this exact same issue on a project. My use case is to read Protobuf messages from Kafka (has ByteArray native payload format) and
ProtobufMessageConverter
is the only converter configured.Following the answer here https://stackoverflow.com/questions/70121156/how-to-use-org-springframework-messaging-converter-protobufmessageconverter have subclassed
ProtobufMessageConverter
.This works OK for binary protobuf encoding, when
content-type: application/x-protobuf
is set. The same converter supports JSON encoding (by checkingprotobufFormatSupport
field) whencontent-type: application/json
is set, but fails with the exception described above.Looking at other MessageConverters, they seem to support multiple payload types, therefore I believe in ProtobufMessageConverter it is a bug.
gnagy commentedon Apr 17, 2024
Another related shortcoming:
If
com.google.protobuf:protobuf-java-util
is on the classpath, thereforeProtobufMessageConverter.protobufJsonFormatPresent
is true, thenProtobufMessageConverter
will try to handlecontent-type: application/json
messages, preventing another JSONMessageConverter
to handle those. This will fail for cases when protobuf binary and non-protobuf JSON topics are configured within the same app. There is no apparent way to configure the supported mime types inProtobufMessageConverter
.siaavush commentedon Jun 25, 2024
I have the exact same problem here
Support byte array payloads in ProtobufMessageConverter