Reputation: 25
How looks my configurations:
Map<String, Object> props = kafkaProperties.buildProducerProperties();
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Which serializer should i use for JSONObject?
Should i send by kafkaTemplate JsonObject to it would be better to send String? (jsonObject.toString());
Upvotes: 1
Views: 1822
Reputation: 9347
It is always recommended to use a specific serializers for Kafka producer instead of using StringSerializer
. The reason is that, string is very generic and it can be either a vaild or invalid JSON string.
If you use StringSerializer
, the KafkaProducer
serializer does not complain if the given string is a valid JSON or not.
So, in future if some developer tries to send a malformed JSON string, it can affect the consumer. Also, the serializer that you would write can be reusable for similar use-cases.
So, better write a new serializer which validates the JSON string (or) write JSONObjectSerializer
or even a serializer for your custom POJO class (if you have any).
public class JSONObjectSerializer implements Serializer<JSONObject> {
public byte[] serialize(String topic, JSONObject data) {
return data.toString().getBytes(); // or whatever is appropriate.
}
}
You anyway have to convert it to JSON, the point is that instead of doing in your main logic (KafkaProducer), you write that in a Serializer
In case you want to convert any object to JSON.
private ObjectMapper objectMapper = new ObjectMapper();
public byte[] serialize(String topic, Object data) {
return objectMapper.writeValueAsBytes(data);
}
Upvotes: 2