jsonspring-mvcjacksonpretty-print

When using Spring MVC for REST, how do you enable Jackson to pretty-print rendered JSON?


While developing REST services using Spring MVC, I would like render JSON 'pretty printed' in development but normal (reduced whitespace) in production.


Solution

  • If you are using Spring Boot 1.2 or later the simple solution is to add

    spring.jackson.serialization.INDENT_OUTPUT=true
    

    to the application.properties file. This assumes that you are using Jackson for serialization.

    If you are using an earlier version of Spring Boot then you can add

    http.mappers.json-pretty-print=true
    

    This solution still works with Spring Boot 1.2 but it is deprecated and will eventually be removed entirely. You will get a deprecation warning in the log at startup time.

    (tested using spring-boot-starter-web)