How to enable Json pretty-print rendered | Jackson + SpringBoot + RestApi

For convenience way to debug output data in development with human-readable output format, JavaSampleApproach shows you how to enable Json pretty-print of Jackson library.

Related posts:
Jackson to convert Java Object to/from Json String
How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– Jackson

II. Problem

When calling a RestApi on browser, we got a non human-readable result for debug:

json pretty print - problems restapi

The same problem when we print json object in console:

json pretty print - problems in console

What we need? -> A good format for reader like as:

json pretty print - expected

– For RestApi, we configure: spring.jackson.serialization.indent_output=true.
– For console printer, we configure ObjectMapper mapper.enable(SerializationFeature.INDENT_OUTPUT)

III. Practice

In the tutorial, we use SpringToolSuite to create a SpringBoot project:

json pretty print - project structure

Step to do:
– Create SpringBoot project
– Create models
– Implement a RestApi
– Configure json pretty-print
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite to create SpringBoot project, then add web dependency:

2. Create models

Create 2 models {Company, Product} that have one-to-many relationship:



3. Implement a RestApi

– Create a Restful Api:

4. Configure json pretty-print

– For RestApi, open file, configure:

– For console printer, in Customer class, using ObjectMapper to re-implement toString function and enable SerializationFeature.INDENT_OUTPUT as below code:

5. Run and check results

Build and Run the SpringBoot project with commanlines: {mvn clean install, mvn spring-boot:run}.
– Make a request: http://localhost:8080/getcompany

-> Result:

json pretty print - expected

IV. Sourcecode


By grokonez | August 20, 2017.

Related Posts

3 thoughts on “How to enable Json pretty-print rendered | Jackson + SpringBoot + RestApi”

  1. I had to replace “underscore” with “hyphen” to make it work:
    from: spring.jackson.serialization.indent_output=true
    to: spring.jackson.serialization.indent-output=true

    Great post. Thank you!

Got Something To Say:

Your email address will not be published. Required fields are marked *