Added setModulesToInstall with convenient Class vararg, renamed setFindModules to setFindModulesViaServiceLoader, made existing setModules override any other setting

Issue: SPR-11040
This commit is contained in:
Juergen Hoeller
2014-01-04 23:29:05 +01:00
parent a2d40f3f16
commit 11b3fe2289

View File

@@ -114,6 +114,8 @@ import org.springframework.util.ClassUtils;
* </bean>
* </pre>
*
* <p>Tested against Jackson 2.2 and 2.3; compatible with Jackson 2.0 and higher.
*
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
* @author Rossen Stoyanchev
* @author Brian Clozel
@@ -136,9 +138,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
private final Map<Object, Boolean> features = new HashMap<Object, Boolean>();
private final List<Module> modules = new LinkedList<Module>();
private List<Module> modules;
private boolean findModules;
private Class<? extends Module>[] modulesToInstall;
private boolean findModulesViaServiceLoader;
private ClassLoader beanClassLoader;
@@ -285,26 +289,44 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
/**
* Set whether to let Jackson find available modules via the ServiceLoader.
* Requires Jackson 2.2 or higher.
* <p>If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself
* will try to find the JSR-310 and Joda-Time support modules on the classpath -
* provided that Java 8 and Joda-Time themselves are available, respectively.
* @see com.fasterxml.jackson.databind.ObjectMapper#findModules()
*/
public void setFindModules(boolean findModules) {
this.findModules = findModules;
}
/**
* Set the list of modules to be registered with the {@link ObjectMapper}.
* Set a complete list of modules to be registered with the {@link ObjectMapper}.
* <p>Note: If this is set, no finding of modules is going to happen - not by
* Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}).
* As a consequence, specifying an empty list here will suppress any kind of
* module detection.
* <p>Specify either this or {@link #setModulesToInstall}, not both.
* @since 4.0
* @see com.fasterxml.jackson.databind.Module
*/
public void setModules(List<Module> modules) {
if (modules != null) {
this.modules.addAll(modules);
}
this.modules = new LinkedList<Module>(modules);
}
/**
* Specify one or more modules by class (or class name in XML),
* to be registered with the {@link ObjectMapper}.
* <p>Modules specified here will be registered in combination with
* Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
* finding of modules (see {@link #setFindModulesViaServiceLoader}).
* <p>Specify either this or {@link #setModules}, not both.
* @since 4.0.1
* @see com.fasterxml.jackson.databind.Module
*/
public void setModulesToInstall(Class<? extends Module>... modules) {
this.modulesToInstall = modules;
}
/**
* Set whether to let Jackson find available modules via the JDK ServiceLoader,
* based on META-INF metadata in the classpath. Requires Jackson 2.2 or higher.
* <p>If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself
* will try to find the JSR-310 and Joda-Time support modules on the classpath -
* provided that Java 8 and Joda-Time themselves are available, respectively.
* @since 4.0.1
* @see com.fasterxml.jackson.databind.ObjectMapper#findModules()
*/
public void setFindModulesViaServiceLoader(boolean findModules) {
this.findModulesViaServiceLoader = findModules;
}
@Override
@@ -342,15 +364,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
configureFeature(feature, this.features.get(feature));
}
if (this.findModules) {
this.objectMapper.registerModules(ObjectMapper.findModules(this.beanClassLoader));
if (this.modules != null) {
// Complete list of modules given
for (Module module : this.modules) {
// Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
this.objectMapper.registerModule(module);
}
}
else {
registerWellKnownModulesIfAvailable();
}
if (!this.modules.isEmpty()) {
this.objectMapper.registerModules(this.modules);
// Combination of modules by class names specified and class presence in the classpath
if (this.modulesToInstall != null) {
for (Class<? extends Module> module : this.modulesToInstall) {
this.objectMapper.registerModule(BeanUtils.instantiate(module));
}
}
if (this.findModulesViaServiceLoader) {
// Jackson 2.2+
this.objectMapper.registerModules(ObjectMapper.findModules(this.beanClassLoader));
}
else {
registerWellKnownModulesIfAvailable();
}
}
}