Pretty print JSON output of Spring Boot Actuator endpoints

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

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

/metrics
/b         


        
14条回答
  •  既然无缘
    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

提交回复
热议问题