I'm trying to convert Map> into JSON Object. I have tried two different ways to convert.
Here is the code looks like but returning null.
@GetMapping("/showRawKafkaMetrics")
public String getMetrics() throws JsonProcessingException {
StringBuilder sb = new StringBuilder();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
.getListenerContainers()) {
// The below map of metrics I'm trying to convert into json Object
Map<String, Map<MetricName, ? extends Metric>> metrics = messageListenerContainer.metrics();
metrics.forEach((clientid, metricMap) -> {
log.info("Client Id:"+sb.toString());
String json = null;
try {
json = objectMapper.writeValueAsString(metricMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
log.info("JSON Metrics :"+json);
metricMap.forEach((metricName, metricValue) -> {
log.info("------------ Metric name: " + metricName.name() + " ----------- Metric value: "
+ metricValue.metricValue() + LINE_BREAK);
});
});
}
String metrics = sb.toString();
return metrics;
}
****2. Using JSON Object
Here is the code looks like:****
@GetMapping("/showRawKafkaMetrics")
public String getMetrics() throws JsonProcessingException {
StringBuilder sb = new StringBuilder();
JsonObject jsonObject = new JsonObject();
for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
.getListenerContainers()) {
// The below map of metrics I'm trying to convert into json Object
// The below map of metrics I'm trying to convert into json Object
Map<String, Map<MetricName, ? extends Metric>> metrics = messageListenerContainer.metrics();
metrics.forEach((clientid, metricMap) -> {
log.info("Client Id:"+sb.toString());
metricMap.forEach((metricName, metricValue) -> {
log.info("------------ Metric name: " + metricName.name() + " ----------- Metric value: "
+ metricValue.metricValue() + LINE_BREAK);
jsonObject.add(metricName.toString(), (JsonElement) metricValue);
log.info("JSON METRICS:"+jsonObject);
});
});
}
String metrics = sb.toString();
return metrics;
}
In both of the cases I'm getting null pointers. Can anyone suggest me best way to convert the Map of Metrics into JsonObject. So further I can process that to create a dashboard.
Let Spring do it for you. Add @ResponseBody
and produces = "application/json"
.
Question is about Map<String, Map<MetricName, ? extends Metric>>
, which is what is returned by messageListenerContainer.metrics()
, but you have a list of those messageListenerContainer
objects, so what you really want is a List
of them, to be converted to JSON.
@GetMapping(path = "/showRawKafkaMetrics", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getMetrics() {
List<Map<String, Map<MetricName, ? extends Metric>>> list = new ArrayList<>();
for (MessageListenerContainer container : kafkaListenerEndpointRegistry.getListenerContainers())
list.add(container.metrics());
return list;
}