2

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;
}
Community
  • 1
  • 1
CFL_Jeff
  • 2,589
  • 3
  • 31
  • 49
  • I think this answer from Keith Donald (Spring) can help you : http://stackoverflow.com/a/10234670/1706698 –  Feb 14 '13 at 16:06
  • @RomainSertelon I don't think I understand his answer. Is he suggesting finding the `MappingJackson2HttpMessageConverter` class in the Spring library and replacing it with the one he posted on GitHub? – CFL_Jeff Feb 14 '13 at 16:15
  • 1
    I think you should configure Spring to use the MappingJackson2HttpMessageConverter. There's something that looks like a good example here : http://stackoverflow.com/questions/12514285/registrer-mappingjackson2httpmessageconverter-in-spring-3-1-2-with-jaxb-annotati –  Feb 15 '13 at 09:14

2 Answers2

5

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.

Matt Crysler
  • 865
  • 3
  • 11
  • 23
1

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()));
}
Matt Crysler
  • 865
  • 3
  • 11
  • 23