Axon 3.0.5 JacksonSerializer not working

Hi

I’m new to axon and I’m having trouble setting up my project to use the JacksonSerializer.
I’m using jackson-datatype-jsr310 version 2.8.9 and axon version 3.0.5.
I’ve setup mongo as the event store engine and it works fine with the xml serializer (I can run a command and the event appears in the db).
When I try use the JacksonSerializer instead it fails with the following log:
o.a.c.g.DefaultCommandGateway : Command 'com.example.domain.contact.command.CreateContactCommand' resulted in org.axonframework.eventsourcing.eventstore.EventStoreException(An event for aggregate [daeca0e8-1eda-4ed4-b0c6-d491b510d1bf] at sequence [0] could not be persisted).

I’ve looked through a few posts related to the JacksonSerializer and it seems quite a lot of changes need to be made to make it work.
One of the things I found said that your events need to have a default constructor, which also means you need to add setters.
Axon recommends however to use immutable objects, so changing all the relevant classes to make them mutable just to make it work with JacksonSerializer doesn’t really make sense.

I’m not sure about everything that needs to be changed as I haven’t yet been able to make it work with JacksonSerializer.

Do people that really want to use json end up polluting their codebase to make it work?

Is there some documentation that lists all the changes you need to make to get JacksonSerializer to work?
I don’t get any stacktraces besides the log I pasted above so it’s kind of difficult to know which part of the application needs to change.

Sorry if I missed something obvious.

Brendan

Hi Brendan,

I’m not so sure about the exception you show, but I can tell you something about the JackonSerializer and the ObjectMapper it uses.

I’ve now seen two approaches to use it:

  1. Add ‘@JsonProperty’ to all your event constructors parameters and to the objects contained in the events. This way the ObjectMapper will now how to correctly de-/serialize it. This was usually the only annotation I needed (so no @JsonCreator was necessary).

  2. You can register the ‘ParameterNamesModule’ to your ObjectMapper, with ‘JsonCreator.Mode.PROPERTIES’ as a constructor parameter. That way, you’ll not need the '@JsonProperty. Jackson still needs, as you’ve pointed out correctly, a default constructor. On the project I’m currently working on we’ve got our events and value objects created in Kotlin, which gives us this default constructor. If you don’t want to use Kotlin, than you could provide the events/valueObjects with private no-arg constructors.

I believe I’ve also seen a Jdk8 module out there, which would also drop the need to pollute your events and value objects with Json annotations.

Anyhow, a default no-arg constructor doesn’t necessarily mean setters.
Just make sure to make it private if you go that route.

Hoping to be of help.
If you’ve got any other questions, do not hesitate to ask them.

Cheers,

Steven

Hi Steven

Thanks for the response, I didn’t realise that you don’t get notified via email when someone replies, thus my late response. We’ve just been focusing on other parts of axon in the meantime seen as we’ve got a lot to learn. I’ll look into your options, so don’t have more questions at the moment, but will see when I’ve tried the different approaches.

cheers

Brendan

Hi Brendan,

If you want to be notified, you can turn it on.
Might be nice to do when you’ve got additional question later on and you want to stay tuned :slight_smile:
Looking forward to your following questions and good luck with the implementation!

Cheers,

Steven