Error serialization of object for kafka Topic in java
Asked Answered
V

1

-2

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:

enter image description here

The conection with kafka works perfect and the topic autocreation are done. enter image description here

you can get the serialization error sending the creation request and then when kafka spring try to serialize the object is when it fails enter image description here

enter image description here

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

Vendue answered 27/4 at 9:4 Comment(0)
B
1

I reviewed the code you committed to your repository and noticed that the apparent issue is with the indentation in your application.yaml configuration file. There is a missing space in your configuration. Please take a careful look at the screenshot below.

Here is the wrong way: wrong

Here is the right way: right

If you're wondering, "How was it able to connect to the server if there was an error?" it's simple: due to the default configuration of Spring Kafka, which uses port 9092 and the local server.

Belshazzar answered 28/4 at 3:23 Comment(2)
you are right and also I am a bit stupid. I dont know since this post is usless for the rest of people if I should complete delete itGavel
Haha, you're definitely not the first and you won't be the last to have this kind of problem.Belshazzar

© 2022 - 2024 — McMap. All rights reserved.