Skip to content
Advertisement

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:

JavaScript

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:

JavaScript

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.

Edit:

Here’s my configuration:

Event producer:

JavaScript

How I send messages:

JavaScript

Event consumer:

JavaScript

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:

JavaScript

Advertisement

Answer

I presume you are using the Jackson2JsonMessageConverter.

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

On the producer side

JavaScript

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement