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);
}
...@@ -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