Commit b98853d8 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #5803 from Grzegorz Poznachowski

* gh-5803:
  Apply standard Jackson2ObjectMapperBuilder config via a customizer
  Make it easier to customize auto-configured Jackson2ObjectMapperBuilder
parents 64e668d5 0c2ecb7b
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jackson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
/**
* Callback interface that can be implemented by beans wishing to further customize the
* {@link ObjectMapper} via {@link Jackson2ObjectMapperBuilder} retaining its default
* auto-configuration.
*
* @author Grzegorz Poznachowski
* @since 1.4.0
*/
public interface Jackson2ObjectMapperBuilderCustomizer {
/**
* Customize the jacksonObjectMapperBuilder.
* @param jacksonObjectMapperBuilder the jacksonObjectMapperBuilder to customize
*/
void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder);
}
...@@ -20,6 +20,7 @@ import java.lang.reflect.Field; ...@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
...@@ -51,6 +52,7 @@ import org.springframework.context.ApplicationContext; ...@@ -51,6 +52,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
...@@ -160,24 +162,70 @@ public class JacksonAutoConfiguration { ...@@ -160,24 +162,70 @@ public class JacksonAutoConfiguration {
@Configuration @Configuration
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class }) @ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
@EnableConfigurationProperties(JacksonProperties.class)
static class JacksonObjectMapperBuilderConfiguration { static class JacksonObjectMapperBuilderConfiguration {
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
private final JacksonProperties jacksonProperties;
JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext, JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext,
JacksonProperties jacksonProperties) { JacksonProperties jacksonProperties,
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.jacksonProperties = jacksonProperties;
} }
@Bean @Bean
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class) @ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() { public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.applicationContext(this.applicationContext); builder.applicationContext(this.applicationContext);
customize(builder, customizers);
return builder;
}
private void customize(Jackson2ObjectMapperBuilder builder,
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
customizer.customize(builder);
}
}
}
@Configuration
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
@EnableConfigurationProperties(JacksonProperties.class)
static class Jackson2ObjectMapperBuilderCustomizerConfiguration {
@Bean
public StandardJackson2ObjectMapperBuilderCustomizer standardJacksonObjectMapperBuilderCustomizer(
ApplicationContext applicationContext,
JacksonProperties jacksonProperties) {
return new StandardJackson2ObjectMapperBuilderCustomizer(applicationContext,
jacksonProperties);
}
private static final class StandardJackson2ObjectMapperBuilderCustomizer
implements Jackson2ObjectMapperBuilderCustomizer, Ordered {
private final ApplicationContext applicationContext;
private final JacksonProperties jacksonProperties;
StandardJackson2ObjectMapperBuilderCustomizer(
ApplicationContext applicationContext,
JacksonProperties jacksonProperties) {
this.applicationContext = applicationContext;
this.jacksonProperties = jacksonProperties;
}
@Override
public int getOrder() {
return 0;
}
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) { if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
builder.serializationInclusion( builder.serializationInclusion(
this.jacksonProperties.getDefaultPropertyInclusion()); this.jacksonProperties.getDefaultPropertyInclusion());
...@@ -194,7 +242,6 @@ public class JacksonAutoConfiguration { ...@@ -194,7 +242,6 @@ public class JacksonAutoConfiguration {
configurePropertyNamingStrategy(builder); configurePropertyNamingStrategy(builder);
configureModules(builder); configureModules(builder);
configureLocale(builder); configureLocale(builder);
return builder;
} }
private void configureFeatures(Jackson2ObjectMapperBuilder builder, private void configureFeatures(Jackson2ObjectMapperBuilder builder,
...@@ -220,9 +267,11 @@ public class JacksonAutoConfiguration { ...@@ -220,9 +267,11 @@ public class JacksonAutoConfiguration {
(DateFormat) BeanUtils.instantiateClass(dateFormatClass)); (DateFormat) BeanUtils.instantiateClass(dateFormatClass));
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
// Since Jackson 2.6.3 we always need to set a TimeZone (see gh-4170) dateFormat);
// If none in our properties fallback to the Jackson's default // Since Jackson 2.6.3 we always need to set a TimeZone (see
// gh-4170). If none in our properties fallback to the Jackson's
// default
TimeZone timeZone = this.jacksonProperties.getTimeZone(); TimeZone timeZone = this.jacksonProperties.getTimeZone();
if (timeZone == null) { if (timeZone == null) {
timeZone = new ObjectMapper().getSerializationConfig() timeZone = new ObjectMapper().getSerializationConfig()
...@@ -238,7 +287,8 @@ public class JacksonAutoConfiguration { ...@@ -238,7 +287,8 @@ public class JacksonAutoConfiguration {
Jackson2ObjectMapperBuilder builder) { Jackson2ObjectMapperBuilder builder) {
// We support a fully qualified class name extending Jackson's // We support a fully qualified class name extending Jackson's
// PropertyNamingStrategy or a string value corresponding to the constant // PropertyNamingStrategy or a string value corresponding to the constant
// names in PropertyNamingStrategy which hold default provided implementations // names in PropertyNamingStrategy which hold default provided
// implementations
String strategy = this.jacksonProperties.getPropertyNamingStrategy(); String strategy = this.jacksonProperties.getPropertyNamingStrategy();
if (strategy != null) { if (strategy != null) {
try { try {
...@@ -267,7 +317,8 @@ public class JacksonAutoConfiguration { ...@@ -267,7 +317,8 @@ public class JacksonAutoConfiguration {
Assert.notNull(field, "Constant named '" + fieldName + "' not found on " Assert.notNull(field, "Constant named '" + fieldName + "' not found on "
+ PropertyNamingStrategy.class.getName()); + PropertyNamingStrategy.class.getName());
try { try {
builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null)); builder.propertyNamingStrategy(
(PropertyNamingStrategy) field.get(null));
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
...@@ -277,7 +328,8 @@ public class JacksonAutoConfiguration { ...@@ -277,7 +328,8 @@ public class JacksonAutoConfiguration {
private void configureModules(Jackson2ObjectMapperBuilder builder) { private void configureModules(Jackson2ObjectMapperBuilder builder) {
Collection<Module> moduleBeans = getBeans(this.applicationContext, Collection<Module> moduleBeans = getBeans(this.applicationContext,
Module.class); Module.class);
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()])); builder.modulesToInstall(
moduleBeans.toArray(new Module[moduleBeans.size()]));
} }
private void configureLocale(Jackson2ObjectMapperBuilder builder) { private void configureLocale(Jackson2ObjectMapperBuilder builder) {
...@@ -295,4 +347,6 @@ public class JacksonAutoConfiguration { ...@@ -295,4 +347,6 @@ public class JacksonAutoConfiguration {
} }
}
} }
...@@ -72,6 +72,7 @@ import static org.mockito.Mockito.mock; ...@@ -72,6 +72,7 @@ import static org.mockito.Mockito.mock;
* @author Marcel Overdijk * @author Marcel Overdijk
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Johannes Edmeier * @author Johannes Edmeier
* @author Grzegorz Poznachowski
*/ */
public class JacksonAutoConfigurationTests { public class JacksonAutoConfigurationTests {
...@@ -419,6 +420,15 @@ public class JacksonAutoConfigurationTests { ...@@ -419,6 +420,15 @@ public class JacksonAutoConfigurationTests {
.isEqualTo("\"Koordinierte Universalzeit\""); .isEqualTo("\"Koordinierte Universalzeit\"");
} }
@Test
public void additionalJacksonBuilderCustomization() throws Exception {
this.context.register(JacksonAutoConfiguration.class,
ObjectMapperBuilderCustomConfig.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
assertThat(mapper.getDateFormat()).isInstanceOf(MyDateFormat.class);
}
@Test @Test
public void parameterNamesModuleIsAutoConfigured() { public void parameterNamesModuleIsAutoConfigured() {
assertParameterNamesModuleCreatorBinding(Mode.DEFAULT, assertParameterNamesModuleCreatorBinding(Mode.DEFAULT,
...@@ -510,6 +520,22 @@ public class JacksonAutoConfigurationTests { ...@@ -510,6 +520,22 @@ public class JacksonAutoConfigurationTests {
} }
@Configuration
protected static class ObjectMapperBuilderCustomConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
jackson2ObjectMapperBuilder.dateFormat(new MyDateFormat());
}
};
}
}
protected static final class Foo { protected static final class Foo {
private String name; private String name;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment