Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pretty Print JSON Per Request With Spring MVC

DZone's Guide to

Pretty Print JSON Per Request With Spring MVC

How to tune the "prettiness" of each pretty-print JSON request, using either GET parameters or an Accept header.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

You will find a lot of posts and StackOverflow answers telling you how to pretty-print JSON responses. But sometimes you may need to tune the “prettiness” per request.

The use case for this is when you are using tools like curl or RESTClient to interact with the system and you want human-readable output. Of course, if you need human-readable output only for debug purposes, you should really consider whether you need JSON at all, or you should use some binary format. But let’s assume you need JSON. And that you’d rather get it pretty-printed, rather than use an external tool to prettify it afterwards.

The basic idea is to enable pretty-printing with either a GET parameter, or preferably with an Accept header like application/json+pretty. With Spring MVC that is not supported out of the box. You’d need to create a class like that:

/**
 * An subclass of the MappingJackson2HttpMessageConverter that accespts the application/json+pretty content type
 * in order to enable per-request prettified JSON responses
 * 
 * @author bozho
 *
 */
public class PrettyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {

 /**
 * Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration
 * provided by {@link Jackson2ObjectMapperBuilder}
 */
 public PrettyMappingJackson2HttpMessageConverter() {
 super();
 objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
 setSupportedMediaTypes(Lists.newArrayList(new MediaType("application", "json+pretty", DEFAULT_CHARSET)));
 }
}

Then in your spring-mvc xml configuraton (or java config counterpart) you should register this as a message converter:

<mvc:message-converters>
 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
 <!-- Handling Accept: application/json+pretty -->
 <bean class="com.yourproject.util.PrettyMappingJackson2HttpMessageConverter" />
</mvc:message-converters>

If you have a separately defined ObjectMapper and want to pass it to the pretty converter, you should override the other constructor (accepting an object mapper), and use the .copy() method before enabling the INDENT_OUTPUT.

And then you’re done. You can switch from regular (non-indented) and pretty output by setting the Accept header to application/json+pretty

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
spring ,spring mvc

Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}