Skip to content

Java RabbitMQ: can’t map message to corresponding class

I have two different java spring projects that communicate through rabbitmq. I’m trying to send a java object but I get this error message: Listener threw exception
Caused by: failed to resolve class name. Class not found [com.thesis.gamamicroservices.productservice.dto.ProductCreatedDTO]

I’m aware that the problem is in the classpath that should be the same in both producer and consumer, but since each project has a different name I can’t make the classpath the same even though both have the exact same class inside the same packages.

The class path is present in the header:


The only difference is that the consumer app is called inventoryservice and not productservice, so it always results in conversion error.

Is there any workaround? My current provisory “solution” is using primitives or java.util classes that I later convert to the target class, but it’s far from ideal since I have cases in which I need to send over 8 different types of objects, I ran out of primitives for my @RabbitHandler’s…

I’ve also made it work by defining the target classpath in the producer but since the message is sent to multiple queues/services it’s not a viable solution here.


Here’s my configuration:

Event producer:

public class EventProducerConfiguration {

    public Exchange createdDeletedExchange() {
       return new FanoutExchange("productCreatedDeletedExchange");

    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        final var rabbitTemplate = new RabbitTemplate(connectionFactory);
        return rabbitTemplate;

    public MessageConverter messageConverter() {
        Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
        DefaultClassMapper classMapper = new DefaultClassMapper();
        classMapper.setIdClassMapping(Map.of("created", ProductCreatedDTO.class));
        return messageConverter;


How I send messages:

rabbitTemplate.convertAndSend(deletedCreatedExchange.getName(), RoutingKeys.CREATED.getNotation(), new ProductCreatedDTO(p, brand.getId(), category.getId()));

Event consumer:

public class EventConsumerConfiguration {

    public FanoutExchange productsPubSubExchange() {
        return new FanoutExchange("productCreatedDeletedExchange");

    public Queue productsQueue() {
        return new Queue("productsInventoryServiceQueue");
    public Binding bindingProducts(@Qualifier("productsExchange") FanoutExchange productsExchange) {
        return BindingBuilder
    public ProductOpsReceiver productEventsReceiver() {
        return new ProductOpsReceiver();

P.s.: I’m not sure if I need to define rabbitTemaplate with the message converter inside the consumer config the same way I did on the producer config, but the two apps are both a consumer and a producer so those bean are defined nonetheless.

How I receive messages:

public class ProductOpsReceiver {

      public void productCreated(ProductCreatedDTO product) {

 , product.getId());



I presume you are using the Jackson2JsonMessageConverter.

You can add type mappings to the converter’s typeMapper.

On the producer side

mapper.setIdClassMapping(Map.of(ProductCreatedDTO.class, "created"));

On the consumer side map the target class to the same token.

The type mapper will consult the map using the type id header (created in this case) to determine which type to use.