Pretty print JSON output of Spring Boot Actuator endpoints

前端 未结 14 862
终归单人心
终归单人心 2020-12-24 12:30

Spring Boot Actuator provides several endpoints to monitor an application as:

/metrics
/b         


        
相关标签:
14条回答
  • 2020-12-24 12:51

    I use Python's commonly installed json.tool module:

    curl --silent http://localhost:8080/metrics | python -mjson.tool
    
    0 讨论(0)
  • 2020-12-24 12:54

    With spring-boot 1.2.6, you need to use:

    spring.jackson.serialization.INDENT_OUTPUT=true
    

    From my log when using the old http.mappers.*:

    http.mappers.json-pretty-print is deprecated. If you are using Jackson, spring.jackson.serialization.INDENT_OUTPUT=true should be used instead.
    
    0 讨论(0)
  • 2020-12-24 12:58

    If you're using gson serialization with Spring, then none of the other answers will work for you. You'll have to use this configuration option:

    spring.gson.pretty-printing=true

    Confirmed working with Spring Boot as of version 2.0.3.Release.

    0 讨论(0)
  • 2020-12-24 12:58

    I use jq for pretty printing JSON as well as filtering it. It's basically sed for JSON. On the mac, it can be installed with homebrew. (https://stedolan.github.io/jq/)

    curl http://localhost:8080/metrics | jq 
    
    0 讨论(0)
  • 2020-12-24 13:09

    Here is my Emacs function to retrieve Spring Actuator Json from endpoints:

    (defvar my/spring-actuator-server-history nil)
    (defvar my/spring-actuator-last-server "http://localhost:8080")
    (defvar my/spring-actuator-path-history nil)
    (defvar my/spring-actuator-path-completion
      '("actuator" "auditevents" "autoconfig" "beans" "configprops" "dump" "env" "flyway" "health" "heapdump"
        "info" "jolokia" "liquibase" "logfile" "loggers" "mappings" "metrics" "shutdown" "trace")))
    
    (defun my/spring-actuator (server path)
      (interactive (list (read-string "Server: " my/spring-actuator-last-server 'my/spring-actuator-server-history)
                         (completing-read "Path: " my/spring-actuator-path-completion nil nil "" 'my/spring-actuator-path-history)))
      (setq my/spring-actuator-last-server server)
      (let ( (bufname (format "actuator: %s" path)) )
        (when (get-buffer bufname)
          (kill-buffer bufname))
        (switch-to-buffer (url-retrieve-synchronously (format "%s/%s" server path)))
        (rename-buffer bufname)
        (goto-char (point-min))
        (re-search-forward "^$" nil 'move)
        (forward-char)
        (delete-region (point-min) (point))
        (json-pretty-print-buffer)
        (json-mode) ))
    

    If you don't like dependency on external json-mode library replace it with js-mode.

    0 讨论(0)
  • 2020-12-24 13:10

    Do the following:

    @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
        }
    
    
    }
    

    This works because Spring Boot uses an ObjectMapper bean to perform all the JSON related operations.

    Note however that this configuration will pretty print all JSON outputs, not just the actuator related stuff.

    UPDATE

    The answer from @DaveSyer is obviously better! I hadn't found the HttpMapperProperties object which is used to configure Jackson. This is it's Javadoc

    0 讨论(0)
提交回复
热议问题