如何将 Map<String, Map<MetricName, ? extends Metric>> 转换为 JsonObject
How to convert Map<String, Map<MetricName, ? extends Metric>> into JsonObject
我正在尝试将 Map> 转换为 JSON 对象。我尝试了两种不同的转换方式。
- 使用 ObjectMapper:
代码如下,但返回 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;
}
在这两种情况下,我得到的都是空指针。谁能建议我将指标映射转换为 JsonObject 的最佳方法。所以我可以进一步处理它来创建一个仪表板。
它不起作用并且您得到 null
的原因是您试图自己序列化 MetricName
和 Metric
类。
我想,带有 ObjectMapper
的示例会抛出一个 JsonProcessingException,它只是在控制台中打印出来,这就是 json
被保留 null
.
的原因
要使用 fasterxml ObjectMapper
准备您自己的 DTO 类 结构,或者只使用 java.util.Map 并用 ObjectMapper#writeValueAsString
序列化它。
参见 FasterXML docs 和示例。
让Spring为您做。添加 @ResponseBody
和 produces = "application/json"
.
问题是关于 Map<String, Map<MetricName, ? extends Metric>>
,这是 messageListenerContainer.metrics()
返回的内容,但是你有一个 messageListenerContainer
对象的列表,所以你真正想要的是 List
个,将转换为 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;
}
我正在尝试将 Map> 转换为 JSON 对象。我尝试了两种不同的转换方式。
- 使用 ObjectMapper:
代码如下,但返回 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;
}
在这两种情况下,我得到的都是空指针。谁能建议我将指标映射转换为 JsonObject 的最佳方法。所以我可以进一步处理它来创建一个仪表板。
它不起作用并且您得到 null
的原因是您试图自己序列化 MetricName
和 Metric
类。
我想,带有 ObjectMapper
的示例会抛出一个 JsonProcessingException,它只是在控制台中打印出来,这就是 json
被保留 null
.
要使用 fasterxml ObjectMapper
准备您自己的 DTO 类 结构,或者只使用 java.util.Map 并用 ObjectMapper#writeValueAsString
序列化它。
参见 FasterXML docs 和示例。
让Spring为您做。添加 @ResponseBody
和 produces = "application/json"
.
问题是关于 Map<String, Map<MetricName, ? extends Metric>>
,这是 messageListenerContainer.metrics()
返回的内容,但是你有一个 messageListenerContainer
对象的列表,所以你真正想要的是 List
个,将转换为 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;
}