114114 * </bean>
115115 * </pre>
116116 *
117+ * <p>Tested against Jackson 2.2 and 2.3; compatible with Jackson 2.0 and higher.
118+ *
117119 * @author <a href="mailto:[email protected] ">Dmitry Katsubo</a> 118120 * @author Rossen Stoyanchev
119121 * @author Brian Clozel
@@ -136,9 +138,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
136138
137139 private final Map <Object , Boolean > features = new HashMap <Object , Boolean >();
138140
139- private final List <Module > modules = new LinkedList <Module >();
141+ private List <Module > modules ;
142+
143+ private Class <? extends Module >[] modulesToInstall ;
140144
141- private boolean findModules ;
145+ private boolean findModulesViaServiceLoader ;
142146
143147 private ClassLoader beanClassLoader ;
144148
@@ -285,26 +289,44 @@ public void setFeaturesToDisable(Object... featuresToDisable) {
285289 }
286290
287291 /**
288- * Set whether to let Jackson find available modules via the ServiceLoader.
289- * Requires Jackson 2.2 or higher.
290- * <p>If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself
291- * will try to find the JSR-310 and Joda-Time support modules on the classpath -
292- * provided that Java 8 and Joda-Time themselves are available, respectively.
293- * @see com.fasterxml.jackson.databind.ObjectMapper#findModules()
292+ * Set a complete list of modules to be registered with the {@link ObjectMapper}.
293+ * <p>Note: If this is set, no finding of modules is going to happen - not by
294+ * Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}).
295+ * As a consequence, specifying an empty list here will suppress any kind of
296+ * module detection.
297+ * <p>Specify either this or {@link #setModulesToInstall}, not both.
298+ * @since 4.0
299+ * @see com.fasterxml.jackson.databind.Module
294300 */
295- public void setFindModules ( boolean findModules ) {
296- this .findModules = findModules ;
301+ public void setModules ( List < Module > modules ) {
302+ this .modules = new LinkedList < Module >( modules ) ;
297303 }
298304
299305 /**
300- * Set the list of modules to be registered with the {@link ObjectMapper}.
301- * @since 4.0
306+ * Specify one or more modules by class (or class name in XML),
307+ * to be registered with the {@link ObjectMapper}.
308+ * <p>Modules specified here will be registered in combination with
309+ * Spring's autodetection of JSR-310 and Joda-Time, or Jackson's
310+ * finding of modules (see {@link #setFindModulesViaServiceLoader}).
311+ * <p>Specify either this or {@link #setModules}, not both.
312+ * @since 4.0.1
302313 * @see com.fasterxml.jackson.databind.Module
303314 */
304- public void setModules (List <Module > modules ) {
305- if (modules != null ) {
306- this .modules .addAll (modules );
307- }
315+ public void setModulesToInstall (Class <? extends Module >... modules ) {
316+ this .modulesToInstall = modules ;
317+ }
318+
319+ /**
320+ * Set whether to let Jackson find available modules via the JDK ServiceLoader,
321+ * based on META-INF metadata in the classpath. Requires Jackson 2.2 or higher.
322+ * <p>If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself
323+ * will try to find the JSR-310 and Joda-Time support modules on the classpath -
324+ * provided that Java 8 and Joda-Time themselves are available, respectively.
325+ * @since 4.0.1
326+ * @see com.fasterxml.jackson.databind.ObjectMapper#findModules()
327+ */
328+ public void setFindModulesViaServiceLoader (boolean findModules ) {
329+ this .findModulesViaServiceLoader = findModules ;
308330 }
309331
310332 @ Override
@@ -342,15 +364,27 @@ public void afterPropertiesSet() {
342364 configureFeature (feature , this .features .get (feature ));
343365 }
344366
345- if (this .findModules ) {
346- this .objectMapper .registerModules (ObjectMapper .findModules (this .beanClassLoader ));
367+ if (this .modules != null ) {
368+ // Complete list of modules given
369+ for (Module module : this .modules ) {
370+ // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
371+ this .objectMapper .registerModule (module );
372+ }
347373 }
348374 else {
349- registerWellKnownModulesIfAvailable ();
350- }
351-
352- if (!this .modules .isEmpty ()) {
353- this .objectMapper .registerModules (this .modules );
375+ // Combination of modules by class names specified and class presence in the classpath
376+ if (this .modulesToInstall != null ) {
377+ for (Class <? extends Module > module : this .modulesToInstall ) {
378+ this .objectMapper .registerModule (BeanUtils .instantiate (module ));
379+ }
380+ }
381+ if (this .findModulesViaServiceLoader ) {
382+ // Jackson 2.2+
383+ this .objectMapper .registerModules (ObjectMapper .findModules (this .beanClassLoader ));
384+ }
385+ else {
386+ registerWellKnownModulesIfAvailable ();
387+ }
354388 }
355389 }
356390
0 commit comments