提交 77abe078 编写于 作者: S Sebastien Deleuze

Set Jackson DEFAULT_VIEW_INCLUSION property to false by default

Issue: SPR-12179
上级 42aef5f5
...@@ -47,6 +47,12 @@ import org.springframework.util.ClassUtils; ...@@ -47,6 +47,12 @@ import org.springframework.util.ClassUtils;
/** /**
* A builder used to create {@link ObjectMapper} instances with a fluent API. * A builder used to create {@link ObjectMapper} instances with a fluent API.
* *
* <p>It customizes Jackson defaults properties with the following ones:
* <ul>
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
* </ul>
*
* <p>Note that Jackson's JSR-310 and Joda-Time support modules will be registered automatically * <p>Note that Jackson's JSR-310 and Joda-Time support modules will be registered automatically
* when available (and when Java 8 and Joda-Time themselves are available, respectively). * when available (and when Java 8 and Joda-Time themselves are available, respectively).
* *
...@@ -384,6 +390,9 @@ public class Jackson2ObjectMapperBuilder { ...@@ -384,6 +390,9 @@ public class Jackson2ObjectMapperBuilder {
this.objectMapper.registerModule(module); this.objectMapper.registerModule(module);
} }
if(!features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
configureFeature(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
} }
......
...@@ -52,6 +52,12 @@ import org.springframework.util.ClassUtils; ...@@ -52,6 +52,12 @@ import org.springframework.util.ClassUtils;
* {@link XmlMapper} ({@code createXmlMapper} property set to true) with setters * {@link XmlMapper} ({@code createXmlMapper} property set to true) with setters
* to enable or disable Jackson features from within XML configuration. * to enable or disable Jackson features from within XML configuration.
* *
* <p>It customizes Jackson defaults properties with the following ones:
* <ul>
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
* </ul>
*
* <p>Example usage with * <p>Example usage with
* {@link MappingJackson2HttpMessageConverter}: * {@link MappingJackson2HttpMessageConverter}:
* *
...@@ -413,6 +419,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper ...@@ -413,6 +419,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.objectMapper.registerModule(module); this.objectMapper.registerModule(module);
} }
if(!features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
configureFeature(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
} }
......
...@@ -80,7 +80,7 @@ public class Jackson2ObjectMapperBuilderTests { ...@@ -80,7 +80,7 @@ public class Jackson2ObjectMapperBuilderTests {
public void defaultProperties() { public void defaultProperties() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build(); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
assertNotNull(objectMapper); assertNotNull(objectMapper);
assertTrue(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS)); assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS)); assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
...@@ -240,7 +240,7 @@ public class Jackson2ObjectMapperBuilderTests { ...@@ -240,7 +240,7 @@ public class Jackson2ObjectMapperBuilderTests {
assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)); assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)); assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
......
...@@ -259,7 +259,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { ...@@ -259,7 +259,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)); assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)); assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
......
...@@ -255,7 +255,7 @@ public class MappingJackson2HttpMessageConverterTests { ...@@ -255,7 +255,7 @@ public class MappingJackson2HttpMessageConverterTests {
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8")); String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertThat(result, containsString("\"withView1\":\"with\"")); assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, containsString("\"withoutView\":\"without\"")); assertThat(result, not(containsString("\"withoutView\":\"without\"")));
} }
@Test @Test
...@@ -288,7 +288,7 @@ public class MappingJackson2HttpMessageConverterTests { ...@@ -288,7 +288,7 @@ public class MappingJackson2HttpMessageConverterTests {
assertThat(result, endsWith(");")); assertThat(result, endsWith(");"));
assertThat(result, containsString("\"withView1\":\"with\"")); assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, not(containsString("\"withView2\":\"with\""))); assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, containsString("\"withoutView\":\"without\"")); assertThat(result, not(containsString("\"withoutView\":\"without\"")));
} }
......
...@@ -126,7 +126,7 @@ public class MappingJackson2XmlHttpMessageConverterTests { ...@@ -126,7 +126,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8")); String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertThat(result, containsString("<withView1>with</withView1>")); assertThat(result, containsString("<withView1>with</withView1>"));
assertThat(result, not(containsString("<withView2>with</withView2>"))); assertThat(result, not(containsString("<withView2>with</withView2>")));
assertThat(result, containsString("<withoutView>without</withoutView>")); assertThat(result, not(containsString("<withoutView>without</withoutView>")));
} }
@Test @Test
......
...@@ -226,7 +226,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase { ...@@ -226,7 +226,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post"); String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
assertTrue(s.contains("\"with1\":\"with\"")); assertTrue(s.contains("\"with1\":\"with\""));
assertFalse(s.contains("\"with2\":\"with\"")); assertFalse(s.contains("\"with2\":\"with\""));
assertTrue(s.contains("\"without\":\"without\"")); assertFalse(s.contains("\"without\":\"without\""));
} }
// SPR-12123 // SPR-12123
......
...@@ -173,8 +173,8 @@ public class MvcNamespaceTests { ...@@ -173,8 +173,8 @@ public class MvcNamespaceTests {
for(HttpMessageConverter<?> converter : converters) { for(HttpMessageConverter<?> converter : converters) {
if(converter instanceof AbstractJackson2HttpMessageConverter) { if(converter instanceof AbstractJackson2HttpMessageConverter) {
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper(); ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
if(converter instanceof MappingJackson2XmlHttpMessageConverter) { if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertEquals(XmlMapper.class, objectMapper.getClass()); assertEquals(XmlMapper.class, objectMapper.getClass());
......
...@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportExtensionTests { ...@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportExtensionTests {
assertEquals(1, adapter.getMessageConverters().size()); assertEquals(1, adapter.getMessageConverters().size());
assertEquals(MappingJackson2HttpMessageConverter.class, adapter.getMessageConverters().get(0).getClass()); assertEquals(MappingJackson2HttpMessageConverter.class, adapter.getMessageConverters().get(0).getClass());
ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)adapter.getMessageConverters().get(0)).getObjectMapper(); ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)adapter.getMessageConverters().get(0)).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter); DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
......
...@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportTests { ...@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportTests {
for(HttpMessageConverter<?> converter : converters) { for(HttpMessageConverter<?> converter : converters) {
if(converter instanceof AbstractJackson2HttpMessageConverter) { if(converter instanceof AbstractJackson2HttpMessageConverter) {
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper(); ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
if(converter instanceof MappingJackson2XmlHttpMessageConverter) { if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertEquals(XmlMapper.class, objectMapper.getClass()); assertEquals(XmlMapper.class, objectMapper.getClass());
......
...@@ -322,7 +322,7 @@ public class RequestResponseBodyMethodProcessorTests { ...@@ -322,7 +322,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString(); String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\"")); assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\"")); assertTrue(content.contains("\"withView2\":\"with\""));
assertTrue(content.contains("\"withoutView\":\"without\"")); assertFalse(content.contains("\"withoutView\":\"without\""));
} }
@Test @Test
...@@ -343,7 +343,7 @@ public class RequestResponseBodyMethodProcessorTests { ...@@ -343,7 +343,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString(); String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\"")); assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\"")); assertTrue(content.contains("\"withView2\":\"with\""));
assertTrue(content.contains("\"withoutView\":\"without\"")); assertFalse(content.contains("\"withoutView\":\"without\""));
} }
// SPR-12149 // SPR-12149
...@@ -366,7 +366,7 @@ public class RequestResponseBodyMethodProcessorTests { ...@@ -366,7 +366,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString(); String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("<withView1>with</withView1>")); assertFalse(content.contains("<withView1>with</withView1>"));
assertTrue(content.contains("<withView2>with</withView2>")); assertTrue(content.contains("<withView2>with</withView2>"));
assertTrue(content.contains("<withoutView>without</withoutView>")); assertFalse(content.contains("<withoutView>without</withoutView>"));
} }
// SPR-12149 // SPR-12149
...@@ -389,7 +389,7 @@ public class RequestResponseBodyMethodProcessorTests { ...@@ -389,7 +389,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString(); String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("<withView1>with</withView1>")); assertFalse(content.contains("<withView1>with</withView1>"));
assertTrue(content.contains("<withView2>with</withView2>")); assertTrue(content.contains("<withView2>with</withView2>"));
assertTrue(content.contains("<withoutView>without</withoutView>")); assertFalse(content.contains("<withoutView>without</withoutView>"));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册