Spring Boot Actuator pretty print JSON

Given that Spring Boot 2 is here for almost a year I want to add a solution for that. Yes, the question targets 1.3.3, but there already are answers for 1.5, and as the title misses the targeted version, I came across this question googling for version 2.1.1, for which we faced the same problem:

While trimmed output is fine for a machine, the screenshot you provided implies you want to read the endpoint's (health's in this case) output in a browser. The solution described at the end of this answer allows both (and more, if required) by replacing the original endpoint with a custom one that "wraps" the original one. By doing so you are allowed to return i.e. pretty printed JSON (note the produces attribute of @ReadOperation) and/or e.g. HTML styled in any way you want.


Despite all the great answers already mentioned, and I wish they would have worked for me, but in spring-boot-starter-1.3.3.RELEASE the only configuration that got my JSON to pretty print was Jenky's answer here: Jackson PrettyPrint for Spring 4

For convenience, I'm copying from that post the two possible solutions, XML config or Code config.

Option 1: XML Configuration

<mvc:annotation-driven>
   <mvc:message-converters register-defaults="false">
       <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
           <property name="objectMapper">
               <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                   <property name="objectMapper">
                       <array>
                           <bean class="com.yourproject.example.CustomObjectMapper"/>
                       </array>
                   </property>
               </bean>
           </property>
       </bean>
   </mvc:message-converters>
</mvc:annotation-driven>

Option 2: Code Configuration

@Configuration
public class CustomWebMvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    protected void extendMessageConverters( List<HttpMessageConverter<?>> converters ) {

        for ( HttpMessageConverter<?> converter : converters ) {

            if ( converter instanceof MappingJackson2HttpMessageConverter ) {

                MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;

                jacksonConverter.setPrettyPrint( true );

            }

        }

    }

}

alexanoid. There are two method to pretty the output:

application.yml

spring:
  jackson:
    serialization:
      INDENT_OUTPUT: true

or

application.properties

spring.jackson.serialization.INDENT_OUTPUT=true

Reference: https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-pretty-json

Both of chrome and curl are works fine.

Pls check the location of application.yml, where are you put it into?

src/main/resources/application.yml

UPDATE

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        System.setProperty("spring.jackson.serialization.INDENT_OUTPUT", "true");
        SpringApplication.run(Application.class, args);
    }
}