1

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?

1
  • 1
    It 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. Commented Dec 30, 2021 at 15:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.