I am trying to serialize an event object to produce a kafka event. All the code of the proyect can be found here:
https://github.com/Gaboxondo/springBootCQRSExample
When I try to publish the event AccountOpenedEvent it gives me the following error:
ERROR 26516 --- [nio-8090-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.apache.kafka.common.errors.SerializationException: Can't convert value of class com.mashosoft.AccountCommand.domain.events.model.AccountOpenedEvent to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer] with root cause
java.lang.ClassCastException: class com.mashosoft.AccountCommand.domain.events.model.AccountOpenedEvent cannot be cast to class java.lang.String (com.mashosoft.AccountCommand.domain.events.model.AccountOpenedEvent is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
I have read a lot about this error but I do not really understand it. I also saw this post but if i am honest I still do not really understand the issue https://www.baeldung.com/java-classcastexception
I understand that maily is a problem due to inheritance and also maybe lombock but still dont know how to solve it
The classes are the following:
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class AccountOpenedEvent extends BaseEvent {
private String accountHolder;
private Date creationDate;
private Double openingBalance;
}
and the Base event abstract class:
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public abstract class BaseEvent {
private String id;
private int version;
}
The configuration for kafka is the following:
kafka:
bootstrap-servers: localhost:9092
producer:
bootstrap-servers: localhost:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
As I said all code is on the github link and is public. Yo can run the docker compose and then just as simple to run the main class:
The conection with kafka works perfect and the topic autocreation are done.
you can get the serialization error sending the creation request and then when kafka spring try to serialize the object is when it fails
Thanks a lot for all your help in advance.
Update: if I make the kafka template configuration manually it works
@Configuration
public class KafkaProducerConfig {
@Bean
public KafkaTemplate<String, BaseEvent> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory(), true);
}
@Bean
public ProducerFactory<String, BaseEvent> producerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put( ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); // Use JsonSerializer for complex objects
return new DefaultKafkaProducerFactory<>(configProps);
}
}
I still would like to know if I am usisng the properties wrong or what I am doing wrong with properties