如何序列化map< string,map< metricname?扩展metric>>以生成metrics json对象

uqdfh47h  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(351)

我试着序列化我的 Map<String, Map<MetricName, ? extends Metric>> 对象使用 Gson 但是我得到的是json字符串响应,而且我还有度量值和名称在里面 Map<MetricName, ? extends Metric> 我的代码如下:

  1. @GetMapping(path = "/showRawKafkaMetrics", produces = MediaType.APPLICATION_JSON_VALUE)
  2. @ResponseBody
  3. public List<String> getMetrics() throws IOException, ClassNotFoundException {
  4. List<String> list = new ArrayList<>();
  5. Gson gson = new Gson();
  6. for (MessageListenerContainer container : kafkaListenerEndpointRegistry.getListenerContainers()){
  7. list.add(gson.toJson(container.metrics()));
  8. }
  9. return list;
  10. }
8i9zcol2

8i9zcol21#

我得到了json字符串响应
好吧,你回来了 List<String> 如果您希望spring返回json,那么为 application/json (你做到了; produces = MediaType.APPLICATION_JSON_VALUE )
如果你希望得到这样的回应

  1. {
  2. "foo": {
  3. "metric": "bar"
  4. }
  5. }

那你就不能回来了 List<?> 因为那是一个物体/Map。
理想情况下,应该为每个返回的类型提供一个具体的响应模型类。

相关问题