Spring Boot Actuator 端点的漂亮打印 JSON 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24503790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pretty print JSON output of Spring Boot Actuator endpoints
提问by Bernhard Woditschka
Spring Boot Actuatorprovides several endpointsto monitoran applicationas:
春季启动器提供了几个端点,以监视的应用程序为:
/metrics
/beans
/health
...
Checking the endpoints with:
使用以下命令检查端点:
curl http://localhost:8080/metrics
results in:
结果是:
{"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}
This is fine for machine consumptionbut hard to readby humans.
这对于机器消费来说很好,但很难被人类阅读。
I'd like to format(i.e. pretty print) the JSONoutput of the Spring Boot Actuatorendpoints to make them easier to read by operations personel.
我想格式化(即漂亮地打印)Spring Boot Actuator端点的JSON输出,以便操作人员更容易阅读它们。
Something like:
就像是:
{
"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,
...
}
I tried setting
我试过设置
http.mappers.json-pretty-print=true
but this setting didn't affect the Actuator output.
但此设置不影响执行器输出。
Is there a configurationto enable pretty printof the Spring Boot Actuator JSONoutput?
是否有一个配置,以实现优质打印的的春天启动器JSON输出?
UPDATE:
更新:
The official sampleworks for me.
该官方样片对我的作品。
It's important to follow the comments from @DaveSyer: the property to set is
遵循@DaveSyer 的评论很重要:要设置的属性是
http.mappers.jsonPrettyPrint=true
Investigation is still under way.
调查仍在进行中。
In the meantime I use the the json pretty print command lineas workaround:
同时,我使用 json 漂亮打印命令行作为解决方法:
Install jsonpp (e.g. for OS X):
安装 jsonpp(例如 OS X):
brew install jsonpp
Then pipe the curl output trough jsonpp which formats the json file on the fly:
然后通过 jsonpp 传输 curl 输出槽,该 jsonpp 动态格式化 json 文件:
curl http://localhost:8080/metrics | jsonpp
Results in:
结果是:
{
"counter.status.200.env": 1,
"counter.status.200.health": 1,
"counter.status.200.info": 2,
"counter.status.200.metrics": 2,
...
}
回答by Bertrand Renuart
As per http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-Hymanson-objectmapper, the official way to enable pretty print with Hymanson in Spring Boot (1.2.2 at least) is to set the following property:
根据http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-Hymanson-objectmapper,启用漂亮打印的官方方法Spring Boot(至少 1.2.2)中的 Hymanson 是设置以下属性:
# Pretty-print JSON responses
spring.Hymanson.serialization.indent_output=true
回答by Witold Kaczurba
For Spring Boot 1.5.1 I have in my YML file:
对于 Spring Boot 1.5.1,我的 YML 文件中有:
spring:
Hymanson:
serialization:
INDENT_OUTPUT: true
@BertrandRenuart answer was the closest, but by IDE did not see indent_output as correct.
@BertrandRenuart 答案是最接近的,但 IDE 认为 indent_output 不正确。
回答by Dave Syer
The "http.mappers" property works for me but I think you might need it camel cased ("jsonPrettyPrint").
“http.mappers”属性对我有用,但我认为你可能需要它是驼峰式的(“jsonPrettyPrint”)。
回答by geoand
Do the following:
请执行下列操作:
@Configuration
public class HymansonConfig {
@Autowired
private ObjectMapper objectMapper; //reuse the pre-configured mapper
@PostConstruct
public void setup() {
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
//whatever else you need
}
}
This works because Spring Boot uses an ObjectMapperbean to perform all the JSON related operations.
这是有效的,因为 Spring Boot 使用一个ObjectMapperbean 来执行所有与 JSON 相关的操作。
Note however that this configuration will pretty print all JSON outputs, not just the actuator related stuff.
但是请注意,此配置将打印所有 JSON 输出,而不仅仅是与执行器相关的内容。
UPDATE
更新
The answer from @DaveSyer is obviously better! I hadn't found the HttpMapperPropertiesobject which is used to configure Hymanson. Thisis it's Javadoc
@DaveSyer 的答案显然更好!我还没有找到HttpMapperProperties用于配置 Hymanson的对象。这是 Javadoc
回答by Alex
Actually I wanted to do the same. But then I asked: why? To debug my service better which comes with a small performance penalty.
其实我也想做同样的事情。但后来我问:为什么?为了更好地调试我的服务,性能损失很小。
Just use a browser extension, like this one:) to get a view like this one
只需使用浏览器扩展程序,就像这个:) 来获得这样的视图
回答by Samuli Pahaoja
I use Python's commonly installed json.toolmodule:
我使用 Python 的常用json.tool模块:
curl --silent http://localhost:8080/metrics | python -mjson.tool
回答by Hakan
With spring-boot 1.2.6, you need to use:
使用 spring-boot 1.2.6,您需要使用:
spring.Hymanson.serialization.INDENT_OUTPUT=true
From my log when using the old http.mappers.*:
从我使用旧的 http.mappers.* 时的日志:
http.mappers.json-pretty-print is deprecated. If you are using Hymanson, spring.Hymanson.serialization.INDENT_OUTPUT=true should be used instead.
回答by forresthopkinsa
If you're using gsonserialization with Spring, then none of the other answers will work for you. You'll have to use this configuration option:
如果您在Spring 中使用gson序列化,那么其他任何答案都不适合您。您必须使用此配置选项:
spring.gson.pretty-printing=true
spring.gson.pretty-printing=true
Confirmed working with Spring Boot as of version 2.0.3.Release.
确认使用 Spring Boot 版本2.0.3.Release。
回答by Seakayone
Instead of using curlI like to use httpieas a http command line client:
而不是使用curl我喜欢httpie用作 http 命令行客户端:
http http://localhost:8080/metrics
http http://localhost:8080/metrics
This would already format and syntax highlight the json response without having to pipe the output into another command. Also the command syntax is a bit more human friendly.
这已经可以格式化和语法突出 json 响应,而无需将输出通过管道传输到另一个命令。此外,命令语法更加人性化。
回答by gregory
I use jqfor pretty printing JSON as well as filtering it. It's basically sedfor JSON. On the mac, it can be installed with homebrew. (https://stedolan.github.io/jq/)
我jq用于漂亮的打印 JSON 以及过滤它。它基本上sed用于 JSON。在 mac 上,它可以用 homebrew 安装。( https://stedolan.github.io/jq/)
curl http://localhost:8080/metrics | jq


