0

Application A writes to a Kafka Topic below User object (json):

public class UserEvent {
   private UUID id; 
   private Object payload; // contains User fields name etc.
}

Application B is trying to consume this User object (User.java resides in application B project) with below application.yml (multiple binders):

    binders:
    azureEventHub:
        type: kafka
        environment:
            spring:
                kafka:
                    bootstrap-servers: servicebus.windows.net:9093
                    consumer:
                        key-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
                        value-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
                        properties:
                            spring.deserializer.key.delegate.class: org.apache.kafka.common.serialization.StringDeserializer
                            spring.deserializer.value.delegate.class: org.springframework.kafka.support.serializer.JsonDeserializer
                            spring.json.trusted.packages: com.*
                            spring.json.value.default.type: com.applicationb.User

Below is how my Spring Cloud Stream Processor class looks:

  @Bean 
  public Function<Message<User>, String> userProcessor()  
  {..  }

But instead of converting the Message payload to "User" class it keeps throwing the error with the
Parent class "UserEvent" not found.

org.springframework.kafka.listener.ListenerExecutionFailedException: 
Listener failed; nested exception is org.springframework.kafka.support.serializer.DeserializationException: 
failed to deserialize; nested exception is org.springframework.messaging.converter.MessageConversionException: 
failed to resolve class name. Class not found [com.abcd.UserEvent]; 
nested exception is java.lang.ClassNotFoundException: com.abcd.UserEvent

Any ideas ?

Roy
  • 1
  • 1
  • You may want to try it against a local Kafka cluster and see if the problem still persists. In that case, please feel free to share a small reproducible sample app for us to triage further. – sobychacko May 25 '22 at 14:22

1 Answers1

0

The deserializer will use type information in the headers before falling back to the default (User).

Also set spring.json.use.type.headers to false.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179