Pretty print JSON response in Spring Boot
In this quick article, we’ll learn how to pretty print JSON response in Spring Boot web application using Jackson property.
Overview
When you create a @RestController in a Spring Boot application to define RESTFul API endpoints then HttpMessageConverters is used to convert Java Object to JSON or XML in order to render the response.
Spring Boot by default render JSON response using MappingJackson2HttpMessageConverter which use Jackson JSON ObjectMapper library.
Rendered JSON response is not pretty print by default but you can enable it with just one property change.
Example
-
We first create a
@RestControllerclass:-@RequestMapping("/posts") public class PostController { @GetMapping public List<Post> getAllPosts() { return Arrays.asList(new Post[] { new Post(1, "post title 1", "post body 1"), new Post(2, "post title 2", "post body 2"), new Post(3, "post title 3", "post body 3") }); } } -
We see that default JSON Response is not pretty print:-
-
Now we add following property depending upon you are using .properties or .yml file:-
application.propertiesspring.jackson.serialization.indent_output = trueapplication.ymlspring: jackson: serialization: indent_output: true -
We restart the application and see that now JSON response is pretty print:-
Why It’s Off by Default
Pretty printing adds indentation whitespace and newlines to every single response. That’s negligible for a one-off demo endpoint, but on a high-traffic API it means extra bytes on every response, adding up across millions of requests and slowing down parsing marginally on the client. Jackson (and therefore Spring Boot) defaults to the compact form so that production APIs pay no such cost unless someone explicitly asks for it.
Scoping It to Development Only
Because this is a normal Spring Boot property, you don’t have to apply it globally — a common pattern is to enable it only for local development and leave production compact. Put the property in a profile-specific file, application-dev.properties, instead of the shared application.properties:
# application-dev.properties
spring.jackson.serialization.indent_output = true
It then only takes effect when the dev profile is active (--spring.profiles.active=dev), and production, which typically doesn’t activate that profile, keeps compact JSON.
Pretty-Printing a Single Response Without the Global Property
If you want indentation for one endpoint only rather than the whole application, you can skip the property entirely and use Jackson’s ObjectMapper directly inside the controller method:
@Autowired
private ObjectMapper objectMapper;
@GetMapping("/posts/pretty")
public String getAllPostsPretty() throws JsonProcessingException {
List<Post> posts = Arrays.asList(new Post[] {
new Post(1, "post title 1", "post body 1")
});
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(posts);
}
writerWithDefaultPrettyPrinter() returns an ObjectWriter configured to indent output, independent of the global SerializationFeature.INDENT_OUTPUT setting — useful when only a debug or admin-facing endpoint needs readable output and the rest of the API should stay compact.
Gotcha: It’s a Global ObjectMapper Setting
spring.jackson.serialization.indent_output configures the auto-configured ObjectMapper bean itself, not just the one controller you’re testing against. Anything in your application that reuses that shared bean — every other @RestController, and any other component that autowires ObjectMapper — starts producing indented JSON too. If you only meant to prettify one endpoint’s output for debugging, reach for the per-response ObjectMapper.writerWithDefaultPrettyPrinter() approach above instead of flipping the global property.
Relax Binding
Please note that spring boot configuration support Relaxed Binding that means properties can be in uppercase or lowercase, both are valid.
spring.jackson.serialization.INDENT_OUTPUT = true
is same as
spring.jackson.serialization.indent_output = true
That’s it for now.
Download the source code for more Jackson related config examples from github/springboot-api