☕ Java & Spring Boot Integration

Java & Spring Boot Integration: Spring Kafka wraps the raw Apache Kafka client the same way Spring Data JPA wraps JDBC: instead of manually opening connections, managing transact

Spring Kafka wraps the raw Apache Kafka client the same way Spring Data JPA wraps JDBC: instead of manually opening connections, managing transactions, and mapping result sets, you annotate a method and Spring wires the entire lifecycle. With `@KafkaListener(topics = "orders")` you get auto-deserialization, consumer group management, error handling, and offset commit — what used to be 40 lines of `KafkaConsumer.poll()` boilerplate becomes 2 lines. But the real question is why you would bother learning the raw Kafka client API at all if Spring wraps it so cleanly. Because when your `@KafkaListener` starts silently dropping messages under load or rebalancing every 30 seconds in CI, the only way to diagnose it is to understand what the annotation is actually doing underneath — `max.poll.records`, `session.timeout.ms`, `enable.auto.commit` are raw client configs that Spring maps to its own properties, and mismatching one causes a consumer that looks healthy in the dashboard but never commits offsets. In Java terms, the relationship is exactly like Spring Data JPA versus raw JDBC: you use the abstraction for productivity, but you must understand the underlying driver to debug the 5% of cases where the abstraction leaks. For QA, this means that any integration test against a `@KafkaListener` component must also verify that offsets were actually committed after processing — a consumer that processes but does not commit is a reprocessing bomb waiting to trigger on the next restart.

pom.xml — Add Spring Kafka dependency

Micro Lab: Code practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

What Does the spring-kafka Dependency Bring Automatically?

When the spring-kafka dependency is added…

When the `spring-kafka` dependency is added, its version is managed by the Spring Boot parent POM — the risk of picking a version incompatible with the Kafka client library disappears.

This single dependency transitively…

This single dependency transitively brings in the `kafka-clients` library too — `KafkaTemplate` and `@KafkaListener` wrap that raw client underneath.

If jackson-databind is NOT added separately…

If `jackson-databind` is NOT added separately, `JsonSerializer`/`JsonDeserializer` cannot convert the message to JSON and the app crashes at startup with `ClassNotFoundException`.

Java analogy: just as Spring Data JPA wraps Hibernate, spring-kafka wraps the raw `KafkaProducer`/`KafkaConsumer` — less code, same underlying mechanism.

application.yml — Kafka configuration

What Raw Kafka Config Does Each application.yml Line Map To?