Make it easier to customize auto-configured Jackson2ObjectMapperBuilder
Previously, it was difficult to customize the auto-configured Jackson2ObjectMapperBuilder. Typically, use of a bean post processor was required. This commit introduces Jackson2ObjectMapperBuilderCustomizer. Beans that implement this interfaces are called during creation of the auto-configured Jackson2ObjectMapperBuilder, providing an opportunity to customize its configuration. Closes gh-5803
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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<Jackson2ObjectMapperBuilderCustomizer> builderCustomizers;
|
||||
|
||||
JacksonObjectMapperConfiguration(
|
||||
ObjectProvider<List<Jackson2ObjectMapperBuilderCustomizer>> 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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user