Home > Software design >  Spring Boot Kafka, package name in the message header
Spring Boot Kafka, package name in the message header

Time:12-09

I have two services eshop and eshop-notification-service. Service eshop sends message: Notification(String message, LocalDateTime timestamp) to Kafka and eshop-notification-service consumes it.

For some reason during deserialization of the message, package name (com.example.eshop.model.Notification) from producer is saved in the message headers, as a result, deserialization on the consumer side is failing with the error:

Caused by: org.springframework.messaging.converter.MessageConversionException: failed to resolve class name. Class not found [com.example.eshop.model.Notification]; nested exception is java.lang.ClassNotFoundException: com.example.eshop.model.Notification

While the correct package path on the consumer side is: com.example.eshopnotification

How can I resolve this issue? Why package is saved in the message headers?

CodePudding user response:

See this answer - How to deserialize message in consumer when produced message is a model type which consumer has no reference to?

You can either use type mapping, or set the USE_TYPE_INFO_HEADERS property to false and set a default type.

You can also set this on the producer side:

JsonSerializer.ADD_TYPE_INFO_HEADERS (default true): You can set it to false to disable this feature on the JsonSerializer (sets the addTypeInfo property).

https://docs.spring.io/spring-kafka/docs/2.7.x/reference/html/#json-serde

CodePudding user response:

try the mapping configuration as below in application.yml

spring: 
  kafka:
    consumer:
      properties:
        spring:
          json:
            type:
              mapping: "com.example.eshop.model.Notification:com.example.eshopnotification.Notification"
  • Related