diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/Jackson2ObjectMapperBuilderCustomizer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/Jackson2ObjectMapperBuilderCustomizer.java new file mode 100644 index 0000000000..79e5d57784 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/Jackson2ObjectMapperBuilderCustomizer.java @@ -0,0 +1,37 @@ +/* + * 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); +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index a58987c8f7..92c6b255cc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -21,6 +21,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Locale; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TimeZone; @@ -41,6 +42,7 @@ import org.joda.time.format.DateTimeFormat; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion; @@ -51,6 +53,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -85,13 +88,30 @@ public class JacksonAutoConfiguration { @ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class }) static class JacksonObjectMapperConfiguration { + private final List builderCustomizers; + + JacksonObjectMapperConfiguration( + ObjectProvider> builderCustomizersProvider) { + this.builderCustomizers = builderCustomizersProvider.getIfAvailable(); + } + @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { + customize(builder); return builder.createXmlMapper(false).build(); } + private void customize(Jackson2ObjectMapperBuilder builder) { + if (this.builderCustomizers != null) { + AnnotationAwareOrderComparator.sort(this.builderCustomizers); + for (Jackson2ObjectMapperBuilderCustomizer customizer : this.builderCustomizers) { + customizer.customize(builder); + } + } + } + } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java index 5e016c8c24..e873432ac8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java @@ -72,6 +72,7 @@ import static org.mockito.Mockito.mock; * @author Marcel Overdijk * @author Sebastien Deleuze * @author Johannes Edmeier + * @author Grzegorz Poznachowski */ public class JacksonAutoConfigurationTests { @@ -419,6 +420,15 @@ public class JacksonAutoConfigurationTests { .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 public void parameterNamesModuleIsAutoConfigured() { assertParameterNamesModuleCreatorBinding(Mode.DEFAULT, @@ -510,6 +520,21 @@ 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 { private String name;