javaxstreamaxon

Can I pass complex objects to command handler and event handler AXON Spring boot


My java version 17 and axon 4.5.3

This is my controller.


 buildOrder = orderService.buildOrder(orderDto.getCartCode(),token);
                  CreateOrderCommand createOrderCommand
                    = CreateOrderCommand.builder()
                    .cartCode(orderDto.getCartCode())
                    .orderStatus("CREATED")
                    .orderCode(buildOrder.getOrderCode())
                    .order(buildOrder)
                    .build();
                    commandGateway.sendAndWait(createOrderCommand);

Here I'm building my order object and passing to gateway. But it returns com.thoughtworks.xstream.converters.ConversionException: No converter available Is there any solution?

I tried by setting these properties in properties file axon: serializer: general: jackson events: jackson messages: jackson


Solution

  • This is not an issue with Axon Framework but with the combination of JDK17 and XStream. As you can see in this GitHub issue of XStream there is even a chance it will not be a viable solution for future versions of the JDK.

    Due to this, Axon Framework will move away from XStream in the next major release (5), to be exact. So, to answer your question, yes, Axon Framework can deal with complex objects such as commands, events, and queries.

    For now, your best bet is to switch the serialization process from XStream to the Jackson implementation. As you're in a Spring Boot application, you could do the following to enforce this:

    // Axon Framework will create an ObjectMapper bean for you, but you can also create your own for further customization.
    @Bean
    @Primary
    public Serializer serializer(ObjectMapper objectMapper) {
        return JacksonSerializer.builder()
                                .objectMapper(objectMapper)
                                // any other builder methods you would want to invoke...
                                .build();
    }
    

    Furthermore, I strongly recommend you use a more recent version of Axon Framework than 4.5.3. When writing this answer, the latest release of Axon Framework is 4.9.1. Especially in 4.5 and 4.6 some fixes have been introduced around serialization that you can (and should) benefit from.