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:
@@ -103,7 +103,7 @@ public abstract class AbstractJackson2View extends AbstractView {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use the default pretty printer when writing JSON.
|
||||
* Whether to use the default pretty printer when writing the output.
|
||||
* This is a shortcut for setting up an {@code ObjectMapper} as follows:
|
||||
* <pre class="code">
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -44,6 +45,8 @@ import org.springframework.web.servlet.View;
|
||||
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
|
||||
* alone via {@link #setExtractValueFromSingleKeyModel}.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.1 and higher.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
@@ -76,10 +79,12 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2JsonView}, setting the content type to {@code application/json}.
|
||||
* Construct a new {@code MappingJackson2JsonView} using default configuration
|
||||
* provided by {@link Jackson2ObjectMapperBuilder} and setting the content type
|
||||
* to {@code application/json}.
|
||||
*/
|
||||
public MappingJackson2JsonView() {
|
||||
super(new ObjectMapper(), DEFAULT_CONTENT_TYPE);
|
||||
super(Jackson2ObjectMapperBuilder.json().build(), DEFAULT_CONTENT_TYPE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.view.json.AbstractJackson2View;
|
||||
@@ -33,6 +34,8 @@ import org.springframework.web.servlet.view.json.AbstractJackson2View;
|
||||
* entry is used. Users can either specify a specific entry in the model via the
|
||||
* {@link #setModelKey(String) sourceKey} property.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.1 and higher.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
@@ -46,8 +49,13 @@ public class MappingJackson2XmlView extends AbstractJackson2View {
|
||||
private String modelKey;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2XmlView} using default configuration
|
||||
* provided by {@link Jackson2ObjectMapperBuilder} and setting the content type
|
||||
* to {@code application/xml}.
|
||||
*/
|
||||
public MappingJackson2XmlView() {
|
||||
super(new XmlMapper(), DEFAULT_CONTENT_TYPE);
|
||||
super(Jackson2ObjectMapperBuilder.xml().build(), DEFAULT_CONTENT_TYPE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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