spring-boot-microservices-series-v2 icon indicating copy to clipboard operation
spring-boot-microservices-series-v2 copied to clipboard

Refactor KafkaListenerConfig to remove unused ObjectMapper and simplify product event handling

Open rajadilipkolli opened this issue 2 months ago • 1 comments

rajadilipkolli avatar Sep 28 '25 11:09 rajadilipkolli

Walkthrough

The Kafka listener now receives a validated ProductDto payload directly (via @Payload @Valid), removing ObjectMapper usage and its constructor dependency; onSaveProductEvent signature and constructor were updated and the DTO is forwarded to the product management service without manual JSON deserialization.

Changes

Cohort / File(s) Summary of Changes
Kafka listener DTO handling
inventory-service/src/main/java/com/example/inventoryservice/config/KafkaListenerConfig.java
Removed ObjectMapper import/field/constructor parameter; updated constructor signature; changed onSaveProductEvent parameter from String to @Payload @Valid ProductDto; removed manual JSON deserialization and pass the ProductDto directly to productManageService; replaced Jackson import with jakarta.validation.Valid.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Kafka as Kafka Broker
  participant Listener as KafkaListenerConfig
  participant Validator as Bean Validation
  participant ProductSvc as ProductManageService

  Kafka->>Listener: ProductDto message (deserialized by Kafka)
  Listener->>Validator: Validate ProductDto (@Valid)
  alt valid DTO
    Listener->>ProductSvc: productManageService.manage(productDto)
    ProductSvc-->>Listener: ack
  else invalid DTO
    Listener-->>Kafka: error handling / reject
  end
sequenceDiagram
  note over Listener: Old flow (before)
  actor Kafka as Kafka Broker
  participant Listener as KafkaListenerConfig
  participant Mapper as ObjectMapper
  participant ProductSvc as ProductManageService

  Kafka->>Listener: String payload (JSON)
  Listener->>Mapper: readValue(String, ProductDto)
  Mapper-->>Listener: ProductDto
  Listener->>ProductSvc: productManageService.manage(productDto)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I hop on queues with whiskers bright,
DTO in paw — no JSON fight. 🥕
Validator nods, the mapping's done,
ObjectMapper naps in burrowed sun.
Hooray—clean payloads, my hop's begun!

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description is entirely missing and does not provide any context or summary of the changes made. Please include a brief description outlining the purpose of the refactor, the removal of ObjectMapper, and the updated event handling to help reviewers understand the scope and intent of the changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly captures the core refactoring of the KafkaListenerConfig class by highlighting the removal of the ObjectMapper and the simplification of product event handling, directly reflecting the main changes in the pull request.
✨ Finishing touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
  • [ ] Commit unit tests in branch listenerpolish

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Sep 28 '25 11:09 coderabbitai[bot]