Pretty print option for Jackson converter and view

Jackson serialization supports pretty printing. Usually it's enabled
by invoking ObjectMapper.configure(..), which is not convenient for
apps with XML configuration. The Jackson HttpMessageConverter and View
now both have a prettyPrint property.

A second more serious issue is documented here:
https://github.com/FasterXML/jackson-databind/issues/12

The workaround discussed at the above link has been implemented.

Issue: SPR-7201
This commit is contained in:
Rossen Stoyanchev
2012-05-09 16:29:22 -04:00
parent 6a162d488a
commit db289495e5
9 changed files with 510 additions and 180 deletions

View File

@@ -37,6 +37,7 @@ import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import com.fasterxml.jackson.core.JsonGenerator;
@@ -143,6 +144,20 @@ public class MappingJackson2JsonViewTest {
validateResult();
}
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
view.render(model, request, response);
String result = response.getContentAsString();
assertTrue("Pretty printing not applied:\n" + result, result.startsWith("{\n \"foo\" : {\n "));
validateResult();
}
@Test
public void renderSimpleBeanPrefixed() throws Exception {

View File

@@ -38,7 +38,6 @@ import org.codehaus.jackson.map.SerializerFactory;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.ser.BeanSerializerFactory;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
@@ -46,6 +45,7 @@ import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
/**
@@ -137,6 +137,20 @@ public class MappingJacksonJsonViewTest {
validateResult();
}
@Test
public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true);
view.render(model, request, response);
String result = response.getContentAsString();
assertTrue("Pretty printing not applied:\n" + result, result.startsWith("{\n \"foo\" : {\n "));
validateResult();
}
@Test
public void renderSimpleBeanPrefixed() throws Exception {