Pretty print JSON output of Spring Boot Actuator endpoints

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

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

/metrics
/b         


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

    As per http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper, the official way to enable pretty print with Jackson in Spring Boot (1.2.2 at least) is to set the following property:

     # Pretty-print JSON responses
     spring.jackson.serialization.indent_output=true
    
    0 讨论(0)
  • 2020-12-24 13:12

    For Spring Boot 1.5.1 I have in my YML file:

    spring:
      jackson:
        serialization:
          INDENT_OUTPUT: true
    

    @BertrandRenuart answer was the closest, but by IDE did not see indent_output as correct.

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

    this not working

    spring.jackson.serialization.INDENT_OUTPUT=true

    this is working spring.jackson.serialization.indent-output=true

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

    Instead of using curl I like to use httpie as a http command line client:

    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.

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

    Unfortunately, the application property

    spring.jackson.serialization.INDENT_OUTPUT

    did not work for me (spring boot versions 1.2.6 to 1.4.0.RELEASE). Instead, in my extension of WebMvcConfigurerAdapter, I've overridden configureMessageConverters() and added my own Jackson2ObjectMapperBuilder:

    @Configuration
    @EnableWebMvc
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
       ...
        private MappingJackson2HttpMessageConverter jacksonMessageConverter() {
            Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                    .featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
                            SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)
                    .featuresToEnable(SerializationFeature.INDENT_OUTPUT).modulesToInstall(hibernate4Module());
            // can use this instead of featuresToEnable(...)
            builder.indentOutput(true);
            return new MappingJackson2HttpMessageConverter(builder.build());
        }
    
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(jacksonMessageConverter());
            super.configureMessageConverters(converters);
        }
    
       ...
    
    }
    

    That seem to do the trick for me on Spring boot 1.4.0.RELEASE and my actuator output is now pretty printed (along with every other json output)

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

    In case somebody with Spring Boot 2 (2.1.1 in my case) stumbles over this question as I did: We faced the same problem, and none of the answers helped for 2.1.1.

    So what we did was to replace the existing endpoint (health in our case) with a new one. I described it at the end of this answer. And yes, this limits our solution to this single endpoint, but on the other hand it has the advantage of being able to format the output in any way you want - including pretty print JSON, but also output styled HTML if requested (by a service technician in a browser in our case). Note the produces attribute of @ReadOperation to achieve that.

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