REST API with Spring: How to Prettify JSON Output
- Details
- Written by Nam Ha Minh
- Last Updated on 28 October 2023   |   Print Email
You know, when developing REST APIs with Java and Spring framework, the JSON output in API response is not formatted (un-indented) by default, for example:
Although this has no issue with JSON parsers, it’s difficult for human (programmers/API developers) to read and understand the API response directly in a web browser or terminal (command line). I think it’d be better to have the JSON output formatted (prettified) at the application level, for the convenient of API developers and consumers.
So, how to prettify the JSON output in API response with Spring Boot?
Well, Spring MVC integrates tightly with Jackson/FasterXML - the Java library for JSON - via an ObjectMapper that does the serialization and deserialization from Java objects to JSON and vice-versa. So we just need to override the default by configuring the ObjectMapper directly via a Spring Bean in the main class as follows:
package net.codejava; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; ... @SpringBootApplication public class RestApiApplication { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); return objectMapper; } public static void main(String[] args) { SpringApplication.run(RestApiApplication.class, args); } }
Then you will see the JSON in API response is indented/formatted as shown in web browser below:
And in terminal/command prompt:
You can also configure the ObjectMapper object in a separate Spring configuration class, as shown in the below example:
package net.codejava; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @Configuration public class AppConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); return objectMapper; } }
However, this won’t work with unit tests for controller layer using @WebMvcTest annotation that doesn’t load configuration classes.
That’s a simple way to prettify JSON output in REST API development with Spring framework and Jackson library, at the application level. I hope you find this post helpful.
You can also watch the following video to see the coding in action:
Related REST API Tutorials:
- Java RESTful Web Services Tutorial for Beginner with Jersey and Tomcat
- Java CRUD RESTful Web Services Examples with Jersey and Tomcat
- Spring Boot Hello World RESTful Web Services Tutorial
- Spring Boot RESTful CRUD API Examples with MySQL database
- Spring Boot File Download and Upload REST API Examples
- Spring Boot REST API CRUD with HATEOAS Tutorial
- How to Use curl for Testing REST APIs (Test CRUD Operations)
- How to Use Postman for Testing REST APIs (Test CRUD Operations)