Details
-
Improvement
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
3.0.0
-
None
-
Unknown
Description
The motivating example here is unmarshalling JAXB from a String message body, but the issue exists for other data formats as well, e.g. JSON. In a route like
from("direct:receive-a-string")
.unmarshal().jaxb()
Camel will unnecessarily serialize the String to bytes, then deserialize the bytes to JAXB Java objects. In addition to being inefficient, this can cause encoding issues if the encoding used by Camel to serialize the String doesn't match the encoding set in the XML declaration.
The reason Camel needs to serialize to bytes is that the DataFormat interface takes an InputStream parameter in the unmarshal method. Because of this parameter, the UnmarshalProcessor needs to convert the message body to bytes before calling the data format.
We should be able to introduce a new method with the signature unmarshal(Exchange), which would let data formats examine the body's type, and decide whether it is necessary to serialize to bytes before unmarshalling. For instance, the JAXB data format could choose only to serialize to bytes if the input body is not one of the types accepted by the JAXB Unmarshaller, or a String.
We should be able to implement this in a backwards compatible way, by introducing the new method as a default method on the DataFormat interface. The default implementation should extract the exchange message body as an InputStream, and call the existing unmarshal method. We can then update UnmarshalProcessor to only call the new method.
Attachments
Issue Links
- is related to
-
CAMEL-21293 camel-core - Unmarshal processor should close InputStream after processing
- Resolved
-
CAMEL-20854 camel-jaxb with camel-cxf: fails to unmarshal after changes in TypeConverter logic
- Resolved