Don’t register Jackson Module beans with all ObjectMappers
Previously, JacksonAutoConfiguration would register any Jackson Module beans with every ObjectMapper found in the application context. This was not consistent with the rest of the Jackson auto-configuration, which is only applied to ObjectMappers that are created or configured via the auto-configured Jackson2ObjectMapperBuilder. This commit removes the code that registers Jackson Module beans with every ObjectMapper. Such beans will still be registered with ObjectMappers created or configured using the auto-configured Jackson2ObjectMapperBuilder. This aligns the configuration of Module with the rest of the ObjectMapper configuration and makes it possible for users to create an ObjectMapper bean and still have complete control over the modules that are registered with it. Closes gh-2489
This commit is contained in:
parent
2a1bca6806
commit
5e4a745620
|
|
@ -23,8 +23,6 @@ import java.util.Collection;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.joda.time.DateTime;
|
||||
|
|
@ -75,19 +73,6 @@ public class JacksonAutoConfiguration {
|
|||
@Autowired
|
||||
private ListableBeanFactory beanFactory;
|
||||
|
||||
@PostConstruct
|
||||
private void registerModulesWithObjectMappers() {
|
||||
Collection<Module> modules = getBeans(this.beanFactory, Module.class);
|
||||
for (ObjectMapper objectMapper : getBeans(this.beanFactory, ObjectMapper.class)) {
|
||||
objectMapper.registerModules(modules);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
|
||||
Class<T> type) {
|
||||
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type).values();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
||||
static class JacksonObjectMapperConfiguration {
|
||||
|
|
@ -249,6 +234,12 @@ public class JacksonAutoConfiguration {
|
|||
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
|
||||
}
|
||||
|
||||
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
|
||||
Class<T> type) {
|
||||
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)
|
||||
.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
|
|
|
|||
|
|
@ -62,9 +62,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link JacksonAutoConfiguration}.
|
||||
|
|
@ -99,18 +97,6 @@ public class JacksonAutoConfigurationTests {
|
|||
assertThat(objectMapper.canSerialize(LocalDateTime.class), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customJacksonModules() throws Exception {
|
||||
this.context.register(ModuleConfig.class, MockObjectMapperConfig.class,
|
||||
JacksonAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
|
||||
@SuppressWarnings({ "unchecked", "unused" })
|
||||
ObjectMapper result = verify(mapper).registerModules(
|
||||
(Iterable<Module>) argThat(hasItem(this.context.getBean("jacksonModule",
|
||||
Module.class))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doubleModuleRegistration() throws Exception {
|
||||
this.context.register(DoubleModulesConfig.class,
|
||||
|
|
|
|||
Loading…
Reference in New Issue