From 2bfc2ecfea7c36620ce6ea5ee6f5c7f052d89173 Mon Sep 17 00:00:00 2001 From: Reshmi <revas.nair@gmail.com> Date: Fri, 2 Feb 2024 20:09:34 +0530 Subject: [PATCH] Fixing type casting issue for userId while reading from kafka event --- .../com/sphere/compentency/kafka/kafkaConsumer.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sphere/compentency/kafka/kafkaConsumer.java b/src/main/java/com/sphere/compentency/kafka/kafkaConsumer.java index 388260f..6e3e45a 100644 --- a/src/main/java/com/sphere/compentency/kafka/kafkaConsumer.java +++ b/src/main/java/com/sphere/compentency/kafka/kafkaConsumer.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component; import java.io.IOException; import java.time.Duration; import java.util.Arrays; +import java.util.List; import java.util.Properties; @Component public class kafkaConsumer { @@ -68,12 +69,20 @@ public class kafkaConsumer { if (msg != null && !msg.isEmpty() && !msg.trim().isEmpty()) { JSONObject json = new JSONObject(record.value()); JSONObject edata = json.getJSONObject("edata"); - String userIds = edata.getString("userIds"); + Object userIdsObject = edata.get("userIds"); + String userId=""; + if (userIdsObject instanceof List) { + List<String> userIds = (List<String>) userIdsObject; + userId = userIds.get(0); + } else { + userId = edata.getString("userIds"); + } + List<String> userIds = (List<String>)edata.get("userIds"); // Now, you can pass userIds to your method JSONObject relatedObject = json.getJSONObject("edata").getJSONObject("related"); String courseId = relatedObject.getString("courseId");//do_1139628834519941121286,do_11394806141846323211 logger.info("Processing Kafka message - userId: {}, courseId: {}", userIds, courseId); - api_services.get_hierarchy(courseId, userIds); + api_services.get_hierarchy(courseId, userId); } else { logger.warn("Received empty or null message from Kafka"); } -- GitLab