json - Spring Boot Actuator 端点的 pretty-print JSON

Spring Boot Actuator 提供了几个端点监控一个应用程序:

/metrics
/beans
/health
...

检查端点:

curl http://localhost:8080/metrics

结果:

{"counter.status.200.env":1,"counter.status.200.health":1,"counter.status.200.info":2,"counter.status.200.metrics":2,"gauge.response.env":5.0,"gauge.response.health":22.0,"gauge.response.info":1.0,"gauge.response.metrics":1.0,"mem":1030144,"mem.free":56118,"processors":8,"uptime":5108095,"instance.uptime":5102906,"heap.committed":1030144,"heap.init":262144,"heap.used":974031,"heap":3728384,"threads.peak":81,"threads.daemon":21,"threads":77,"classes":8854,"classes.loaded":8860,"classes.unloaded":6,"gc.ps_scavenge.count":119,"gc.ps_scavenge.time":7223,"gc.ps_marksweep.count":12,"gc.ps_marksweep.time":17573}

适合机器使用,但很难被人类阅读

我想格式化(即 pretty-print )Spring Boot Actuator 端点的 JSON 输出,使其更易于阅读由操作人员。

类似:

{
  "counter.status.200.env":1,
  "counter.status.200.health":1,
  "counter.status.200.info":2,
  "counter.status.200.metrics":2,
  "gauge.response.env":5.0,
  "gauge.response.health":22.0,
  "gauge.response.info":1.0,
  ...
}

我试过设置

http.mappers.json-pretty-print=true 

但此设置不影响执行器输出。

是否有配置启用 pretty-print Spring Boot Actuator JSON输出?

更新:

official sample为我工作。

关注@DaveSyer 的评论很重要:要设置的属性是

http.mappers.jsonPrettyPrint=true

调查仍在进行中。

与此同时,我使用 json pretty-print 命令行 作为解决方法:

安装 jsonpp(例如用于 OS X):

brew install jsonpp

然后将 curl 输出通过 jsonpp 管道传输,它会即时格式化 json 文件:

curl http://localhost:8080/metrics | jsonpp

结果:

{
  "counter.status.200.env": 1,
  "counter.status.200.health": 1,
  "counter.status.200.info": 2,
  "counter.status.200.metrics": 2,
  ...
}

最佳答案

根据 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper ,在 Spring Boot(至少 1.2.2)中使用 Jackson 启用 pretty-print 的官方方法是设置以下属性:

 # Pretty-print JSON responses
 spring.jackson.serialization.indent_output=true

关于json - Spring Boot Actuator 端点的 pretty-print JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503790/

相关文章:

json - 从 PostgreSQL 中的函数返回中删除双引号

python - 如何从一个文件中提取多个 JSON 对象?

javascript - 如何在 console.log 中打印 JSON 数据?

json - 如何自定义来自 spring @Valid 验证的默认错误消息?

python - json.dumps 打乱了顺序

iphone - 从 NSArray 创建一个 json 字符串

json - HTTPResponse 对象 -- JSON 对象必须是 str,而不是 'byte

python - 如果键可能不存在,则从 Python dict 读取

python - 避免 KeyError 的默认字典键

json - 以 JSON 格式访问(新型、公共(public))Google 工作表