Spring Boot Actuator端点的漂亮打印JSON输出

Ber*_*hka 27 spring json spring-boot

春季启动器提供了几个端点,以监视应用程序为:

/metrics
/beans
/health
...
Run Code Online (Sandbox Code Playgroud)

检查端点:

curl http://localhost:8080/metrics
Run Code Online (Sandbox Code Playgroud)

结果是:

{"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}
Run Code Online (Sandbox Code Playgroud)

对机器消耗很好,很难人类阅读.

我想格式化(即打印)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,
  ...
}
Run Code Online (Sandbox Code Playgroud)

我尝试过设置

http.mappers.json-pretty-print=true 
Run Code Online (Sandbox Code Playgroud)

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

是否有一个配置,以实现优质打印的的春天启动器JSON输出?

更新:

官方样片对我的作品.

遵循@DaveSyer的评论非常重要:要设置的属性是

http.mappers.jsonPrettyPrint=true
Run Code Online (Sandbox Code Playgroud)

调查仍在进行中.

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

安装jsonpp(例如OS X):

brew install jsonpp
Run Code Online (Sandbox Code Playgroud)

然后通过jsonpp管理curl输出,jsonpp可以动态格式化json文件:

curl http://localhost:8080/metrics | jsonpp
Run Code Online (Sandbox Code Playgroud)

结果是:

{
  "counter.status.200.env": 1,
  "counter.status.200.health": 1,
  "counter.status.200.info": 2,
  "counter.status.200.metrics": 2,
  ...
}
Run Code Online (Sandbox Code Playgroud)

Ber*_*art 54

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

 # Pretty-print JSON responses
 spring.jackson.serialization.indent_output=true
Run Code Online (Sandbox Code Playgroud)


Wit*_*rba 14

对于Spring Boot 1.5.1我在我的YML文件中:

spring:
  jackson:
    serialization:
      INDENT_OUTPUT: true
Run Code Online (Sandbox Code Playgroud)

@BertrandRenuart答案是最接近的,但是IDE没有看到indent_output是正确的.


Dav*_*yer 10

"http.mappers"属性适用于我,但我认为你可能需要它来骆驼("jsonPrettyPrint").


geo*_*and 7

请执行下列操作:

@Configuration
public class JacksonConfig {

    @Autowired
    private ObjectMapper objectMapper; //reuse the pre-configured mapper


    @PostConstruct
    public void setup() {
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        //whatever else you need
    }


}
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为Spring Boot使用ObjectMapperbean来执行所有与JSON相关的操作.

但请注意,此配置将打印所有JSON输出,而不仅仅是与执行器相关的内容.

UPDATE

@DaveSyer的答案显然更好!我没有找到HttpMapperProperties用于配置Jackson 的对象.是Javadoc


Ale*_*lex 5

其实我也想这样做。但是后来我问:为什么?为了更好地调试我的服务,这会降低性能。

只需使用一个浏览器扩展程序,例如::)即可获得这样的视图

在此处输入图片说明