我尝试序列化我的 Map<String, Map<MetricName, ? extends Metric>>
对象使用 Gson
但我收到 JSON 字符串响应。而且我在内部 Map<MetricName, ? extends Metric>
中有指标值和名称我的代码如下所示:
@GetMapping(path = "/showRawKafkaMetrics", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> getMetrics() throws IOException, ClassNotFoundException {
List<String> list = new ArrayList<>();
Gson gson = new Gson();
for (MessageListenerContainer container : kafkaListenerEndpointRegistry.getListenerContainers()){
list.add(gson.toJson(container.metrics()));
}
return list;
}
最佳答案
I'm getting JSON String response
好吧,你回来了List<String>
如果您希望 Spring 返回 JSON,请添加 application/json
的响应映射(你做到了;produces = MediaType.APPLICATION_JSON_VALUE
)
如果您期待类似的回复
{
"foo": {
"metric": "bar"
}
}
那么你就无法返回 List<?>
因为那是一个对象/ map 。
理想情况下,您应该为每个返回类型都有一个具体的响应模型类。
关于java - 如何序列化 Map<String, Map<MetricName, ?扩展 Metric>> 生成 Metrics Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61311959/