In camel 2.x there was marshal().serialization(). It’s no longer there in camel 3.x. Is Java object serialization possible in camel 3?
-
1It seems the Java object serialization was removed from the 3.x documentation as it no longer exists under the Data Formats section. Funny there is no mention of the change in the 2.x to 3.x migration documentation. Have you actually tried it with 3.x? If it really is gone from the code (camel-core), then I guess the fallback is to write a simple processor to do it in place of the marshal().serialization() bit.Gerry Mantha– Gerry Mantha2021-12-30 15:49:10 +00:00Commented Dec 30, 2021 at 15:49
1 Answer
You can write your own Processor, Bean, DataFormat, or TypeConverter to do so. Here is a way to do this using a bean that you can then reuse anywhere in your Camel routes:
@Component
public class Serializer {
@Handler
public String serializeToBase64String(@Body Serializable inputObject) {
Assert.notNull(inputObject, "Object in Exchange Body must implement Serializable!");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(inputObject);
} catch (IOException e) {
// ...
}
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
}
Then to use in your Camel route, just use:
.bean("serializer")
Instead of:
.marshal().serialization()
That being said if you can help it, avoid serializing any Java object for long term storage or for communication between applications. The serialized class may have had changed after being stored and no longer work. If used for communication it adds an unacceptable amount of coupling between applications. Not only are we forcing the applications to be implemented using the same language (Java) but even the same version of classes intended to be serialized/deserialized, forcing both to share a domain-specific model library.
Instead if you are able to, preferably use JSON, XML, or some other form of language/class-agnostic communication to reduce coupling.