0

I'm trying to deserialize an object using Kafka inside a Quarkus Project JDK-17. But It simply won't work. I was having the same issue when serializing the message inside the same project, but using ObjectMapper().registerModule(new JavaTimeModule()); solved the issue.

Now that I need to consume the same object, inside the same project, It won't work. I've I already tried the following:

new ObjectMapper().registerModule(new JavaTimeModule());
mapper.readValue(data, BudgetToMarinaOrderDTO.class);

2:

new ObjectMapper().registerModule(new JavaTimeModule()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.readValue(data, BudgetToMarinaOrderDTO.class);

3:

Gson gson = new GsonBuilder().create();
gson.fromJson(data, BudgetToMarinaOrderDTO.class);

4:

gsonBuilder.registerTypeAdapter(LocalDateTime.class, new CustomLocalDateTimeDeserializer());
Gson gson = gsonBuilder.setPrettyPrinting().create();
gson.fromJson(data, BudgetToMarinaOrderDTO.class);

These are the related dependencies that I'm using:

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10</version>
        </dependency>

Each one of those gives me a different Error, but they all talk about the same thing... Here's one of these errors:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.time.LocalDateTime` from Object value (
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
rafaelpadu
  • 1,134
  • 2
  • 6
  • 20
  • 1
    Your error is from Jackson, so why are you trying to use Gson? Creating a new ObjectMapper will not fix the builtin Kafka JSON Deserializer. Call `mapper.registerModule`, not make a brand new one – OneCricketeer Dec 17 '22 at 20:28
  • 1
    You're right. But the issue was the serialization in the producer. I was using Gson.toJson() function. When I changed it to ObjectMapper.registerModule(new JavaTimeModule()).writeValueAsString() worked just fine! – rafaelpadu Dec 19 '22 at 12:46
  • Feel free to put your working code below as an a answer – OneCricketeer Dec 19 '22 at 12:51

0 Answers0