Can I configure Jackson JSON pretty printing from annotations or from Spring MVC controller?

I'm using Jackson 1.9.6 (codehaus) for JSON serialization of my response bodies in a Spring MVC application, and I'm having trouble finding a way to configure pretty printing. All of the code examples I've been able to find (like this and this) involve playing with an instantiation of ObjectMapper or ObjectWriter, but I don't currently use an instantiation of these for anything else. I wouldn't even know where to put this code. All of my Jackson configurations are taken care of by annotating the POJOs being serialized to JSON.

Is there a way to specify pretty printing in an annotation? I would think they would have put that in @JsonSerialize, but it doesn't look like it.

My class to be serialized looks like this:

@JsonAutoDetect @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public class JSONObject implements Serializable{...}

and my Spring controller method looks like this:

@RequestMapping(method = RequestMethod.GET) public @ResponseBody List<Object> getMessagesAndUpdates(HttpServletRequest request, HttpServletResponse response) { JSONObject jsonResponse = new JSONObject(); . . . //this will generate a non-pretty-printed json response. I want it to be pretty-printed. return jsonResponse; }


I searched and searched for something similar and the closest I could find was adding this bean to my Application context configuration (NOTE: I am using Spring Boot so I am not 100% certain this will work as-is in a non-Spring Boot app):

@Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true); return builder; }

In my opinion, its the cleanest available solution and works pretty well.


Adding this as a separate answer so I can format the output.

As luck would have it, the non-Spring Boot solution wasn't too far from the Spring Boot solution :)

@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd")); converters.add(new MappingJackson2HttpMessageConverter(builder.build())); converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); }


  • Simple json structure throwing error when converting to pojo
  • QueryDsl and @JsonAutoDetect - Q classes not generated
  • Android JSON Parsing And Conversion
  • is there a generic Map strategy for Apache Camel's Bindy format?
  • Datasnap RESTful JSON Result About?
  • Datasnap RESTful JSON Result About?
  • Python - Convenient way to check for a value in a list that is part of a dictionary?
  • deserialize json array to java object using Jackson
  • Selecting elements with lodash where inner properties match values in an array
  • JSON to Objects in java?
  • Does a SQL Server Computed Column require “persistence” in order for it to be indexed efficiently?
  • How to properly define own type of class in JSDoc?
  • bty = “n” in ggplot2
  • Pipe for multiple processes
  • File path validation in javascript
  • Python - replace unicode emojis with ASCII charachters
  • Why do native C++ projects have a TargetFrameworkVersion?
  • Filtering SPARQL results by day and month
  • How java deals with references to a generic type
  • Gzip http compression problem on iis7
  • Is an if-let or a normal if condition better?
  • Why not Factory pattern for sorting? [closed]
  • Rails redirect_to another controller method throwing “Template missing”
  • How to use the resource module to measure the running time of a function?
  • Should a web service response include empty values?
  • SSL client cert authentication for only some URLs?
  • What is Eclipse's Declaration View used for?
  • Redux, normalised entities and lodash merge
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • How to get Windows thread pool to call class member function?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?