Create a builder for Jackson ObjectMapper
Jackson2ObjectMapperBuilder now allows to create ObjectMapper and XmlMapper instances easily thanks to its fluent API. This builder is used in Jackson message converters and views to instantiate default ObjectMapper and XmlMapper. This commit also add a createXmlMapper property to Jackson2ObjectMapperFactoryBean in order to allow to create easily a XmlMapper instance. Issue: SPR-12243
This commit is contained in:
@@ -78,13 +78,13 @@ public class AnnotationDrivenBeanDefinitionParserTests {
|
||||
|
||||
@Test
|
||||
public void testPathMatchingConfiguration() {
|
||||
loadBeanDefinitions("mvc-config-path-matching.xml");
|
||||
RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertNotNull(hm);
|
||||
loadBeanDefinitions("mvc-config-path-matching.xml");
|
||||
RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
|
||||
assertNotNull(hm);
|
||||
assertTrue(hm.useSuffixPatternMatch());
|
||||
assertFalse(hm.useTrailingSlashMatch());
|
||||
assertTrue(hm.useRegisteredSuffixPatternMatch());
|
||||
assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class));
|
||||
assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class));
|
||||
assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class));
|
||||
List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
|
||||
assertThat(fileExtensions, Matchers.contains("xml"));
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -44,6 +48,8 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockRequestDispatcher;
|
||||
@@ -162,8 +168,19 @@ public class MvcNamespaceTests {
|
||||
assertNotNull(adapter);
|
||||
assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));
|
||||
|
||||
List<HttpMessageConverter<?>> messageConverters = adapter.getMessageConverters();
|
||||
assertTrue(messageConverters.size() > 0);
|
||||
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
|
||||
assertTrue(converters.size() > 0);
|
||||
for(HttpMessageConverter<?> converter : converters) {
|
||||
if(converter instanceof AbstractJackson2HttpMessageConverter) {
|
||||
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
|
||||
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
|
||||
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
|
||||
assertEquals(XmlMapper.class, objectMapper.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull(appContext.getBean(FormattingConversionServiceFactoryBean.class));
|
||||
assertNotNull(appContext.getBean(ConversionService.class));
|
||||
|
||||
@@ -16,14 +16,15 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -72,6 +73,8 @@ import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* A test fixture with a sub-class of {@link WebMvcConfigurationSupport} that also
|
||||
* implements the various {@link WebMvcConfigurer} extension points.
|
||||
@@ -157,6 +160,14 @@ public class WebMvcConfigurationSupportExtensionTests {
|
||||
|
||||
// Message converters
|
||||
assertEquals(1, adapter.getMessageConverters().size());
|
||||
assertEquals(MappingJackson2HttpMessageConverter.class, adapter.getMessageConverters().get(0).getClass());
|
||||
ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)adapter.getMessageConverters().get(0)).getObjectMapper();
|
||||
assertTrue(objectMapper.getDeserializationConfig()
|
||||
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getSerializationConfig()
|
||||
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getDeserializationConfig()
|
||||
.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
|
||||
|
||||
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
|
||||
|
||||
|
||||
@@ -21,6 +21,10 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
@@ -36,6 +40,9 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.FormattingConversionService;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -150,7 +157,19 @@ public class WebMvcConfigurationSupportTests {
|
||||
public void requestMappingHandlerAdapter() throws Exception {
|
||||
ApplicationContext context = initContext(WebConfig.class);
|
||||
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
|
||||
assertEquals(9, adapter.getMessageConverters().size());
|
||||
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
|
||||
assertEquals(9, converters.size());
|
||||
for(HttpMessageConverter<?> converter : converters) {
|
||||
if(converter instanceof AbstractJackson2HttpMessageConverter) {
|
||||
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
|
||||
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
|
||||
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
|
||||
assertEquals(XmlMapper.class, objectMapper.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
|
||||
assertNotNull(initializer);
|
||||
|
||||
Reference in New Issue
Block a user