I have a question regarding consuming JSON String from Kafka: Do I need to add a Config class for my consumer for Deserialization if I'm just consuming a String and then mapping it to a DTO?
@Service
public class Consumer {
private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
@Autowired
OBTMapper obtMapper;
@KafkaListener(topics = "foo" , groupId = "group_id")
public void receive(String message) {
//Map String to DTO
obtMapper.Mapper(message);
logger.info("Message received %s", message);
}
When do I have to add this config class in general? I know that when I want to send an object I need to serialize it first in the Producer config and send it and then deserialize it in the consumer but do I also need that if I'm just producing a String and consuming it afterwards?
CodePudding user response:
Only binary data pass through Kafka, so yes, we must always serialize when producing and deserialize when consuming.
CodePudding user response:
You don't need to wire in your own ObjectMapper, but yes, you need a deserializer. Jackson ObjectMapper both deserializes string/bytes and parses that, though.
Spring Kafka natively offers JSON deserialization. Then you can use your POJO in the method parameter rather than just a String
https://docs.spring.io/spring-kafka/reference/html/#json-serde