So far, we have seen how to produce and consume JSON messages using plain Java and Jackson. We will see here how to create our custom serializers and deserializers.
We have seen how to use StringSerializer in the producer and StringDeserializer in the consumer. Now, we will see how to build our own SerDe to abstract the serialization/deserialization processes away from the core code of the application.
To build a custom serializer, we need to create a class that implements the org.apache.kafka.common.serialization.Serializer interface. This is a generic type, so we can indicate the custom type to be converted into an array of bytes (serialization).
In the src/main/java/kioto/serde directory, create a file called HealthCheckSerializer.java with the content of Listing 4.11.
The following is the content of Listing 4.11, HealthCheckSerializer.java:
package kioto...