Class Jackson2ObjectMapperBuilder
ObjectMapper instances with a fluent API.
 It customizes Jackson's default properties with the following ones:
- MapperFeature.DEFAULT_VIEW_INCLUSIONis disabled
- DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESis disabled
It also automatically registers the following well-known modules if they are detected on the classpath:
- jackson-datatype-jdk8:
 support for other Java 8 types like Optional
- jackson-datatype-jsr310: support for Java 8 Date & Time API types
- jackson-module-kotlin: support for Kotlin classes and data classes
- jackson-modules-java8/parameter-names: support for accessing parameter names
- Since:
- 4.1.1
- Author:
- Sebastien Deleuze, Juergen Hoeller, Tadaya Tsuyukubo, EddĂș MelĂ©ndez
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionannotationIntrospector(AnnotationIntrospector annotationIntrospector) Set anAnnotationIntrospectorfor both serialization and deserialization.annotationIntrospector(Function<AnnotationIntrospector, AnnotationIntrospector> pairingFunction) Alternative toannotationIntrospector(AnnotationIntrospector)that allows combining with rather than replacing the currently set introspector, e.g.applicationContext(ApplicationContext applicationContext) Set the SpringApplicationContextin order to autowire Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).autoDetectFields(boolean autoDetectFields) Shortcut forMapperFeature.AUTO_DETECT_FIELDSoption.autoDetectGettersSetters(boolean autoDetectGettersSetters) <T extends ObjectMapper>
 Tbuild()Build a newObjectMapperinstance.static Jackson2ObjectMapperBuildercbor()Obtain aJackson2ObjectMapperBuilderinstance in order to build a CBOR data formatObjectMapperinstance.voidconfigure(ObjectMapper objectMapper) Configure an existingObjectMapperinstance with this builder's settings.createXmlMapper(boolean createXmlMapper) If set totrue, anXmlMapperwill be created using its default constructor.dateFormat(DateFormat dateFormat) Define the format for date/time with the givenDateFormat.defaultTyping(TypeResolverBuilder<?> typeResolverBuilder) Specify aTypeResolverBuilderto use for Jackson's default typing.defaultUseWrapper(boolean defaultUseWrapper) Define if a wrapper will be used for indexed (List, array) properties or not by default (only applies toXmlMapper).defaultViewInclusion(boolean defaultViewInclusion) Shortcut forMapperFeature.DEFAULT_VIEW_INCLUSIONoption.deserializerByType(Class<?> type, JsonDeserializer<?> deserializer) Configure a custom deserializer for the given type.deserializers(JsonDeserializer<?>... deserializers) Configure custom deserializers.deserializersByType(Map<Class<?>, JsonDeserializer<?>> deserializers) Configure custom deserializers for the given types.factory(JsonFactory factory) Define theJsonFactoryto be used to create theObjectMapperinstance.failOnEmptyBeans(boolean failOnEmptyBeans) Shortcut forSerializationFeature.FAIL_ON_EMPTY_BEANSoption.failOnUnknownProperties(boolean failOnUnknownProperties) Shortcut forDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESoption.featuresToDisable(Object... featuresToDisable) Specify features to disable.featuresToEnable(Object... featuresToEnable) Specify features to enable.filters(FilterProvider filters) Set the global filters to use in order to support@JsonFilterannotated POJO.findModulesViaServiceLoader(boolean findModules) Set whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath.handlerInstantiator(HandlerInstantiator handlerInstantiator) Customize the construction of Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).indentOutput(boolean indentOutput) Shortcut forSerializationFeature.INDENT_OUTPUToption.static Jackson2ObjectMapperBuilderjson()Obtain aJackson2ObjectMapperBuilderinstance in order to build a regular JSONObjectMapperinstance.Override the defaultLocaleto use for formatting.Override the defaultLocaleto use for formatting.Add mix-in annotations to use for augmenting specified class or interface.Add mix-in annotations to use for augmenting specified class or interface.moduleClassLoader(ClassLoader moduleClassLoader) Set the ClassLoader to use for loading Jackson extension modules.Specify the modules to be registered with theObjectMapper.Variant ofmodules(Module...)with aConsumerfor full control over the underlying list of modules.Variant ofmodules(Module...)with aList.modulesToInstall(Module... modules) Specify one or more modules to be registered with theObjectMapper.modulesToInstall(Class<? extends Module>... modules) Specify one or more modules by class to be registered with theObjectMapper.modulesToInstall(Consumer<List<Module>> consumer) Variant ofmodulesToInstall(Module...)with aConsumerfor full control over the underlying list of modules.postConfigurer(Consumer<ObjectMapper> configurer) An option to apply additional customizations directly to theObjectMapperinstances at the end, after all other config properties of the builder have been applied.propertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) Specify aPropertyNamingStrategyto configure theObjectMapperwith.serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include inclusion) Set a custom inclusion strategy for serialization.serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Value serializationInclusion) Set a custom inclusion strategy for serialization.serializerByType(Class<?> type, JsonSerializer<?> serializer) Configure a custom serializer for the given type.serializers(JsonSerializer<?>... serializers) Configure custom serializers.serializersByType(Map<Class<?>, JsonSerializer<?>> serializers) Configure custom serializers for the given types.simpleDateFormat(String format) Define the date/time format with aSimpleDateFormat.static Jackson2ObjectMapperBuildersmile()Obtain aJackson2ObjectMapperBuilderinstance in order to build a Smile data formatObjectMapperinstance.Override the defaultTimeZoneto use for formatting.Override the defaultTimeZoneto use for formatting.visibility(com.fasterxml.jackson.annotation.PropertyAccessor accessor, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility visibility) Specify visibility to limit what kind of properties are auto-detected.static Jackson2ObjectMapperBuilderxml()Obtain aJackson2ObjectMapperBuilderinstance in order to build anXmlMapperinstance.
- 
Constructor Details- 
Jackson2ObjectMapperBuilderpublic Jackson2ObjectMapperBuilder()
 
- 
- 
Method Details- 
createXmlMapperIf set totrue, anXmlMapperwill be created using its default constructor. This is only applicable tobuild()calls, not toconfigure(com.fasterxml.jackson.databind.ObjectMapper)calls.
- 
factoryDefine theJsonFactoryto be used to create theObjectMapperinstance.- Since:
- 5.0
 
- 
dateFormatDefine the format for date/time with the givenDateFormat.Note: Setting this property makes the exposed ObjectMappernon-thread-safe, according to Jackson's thread safety rules.- See Also:
 
- 
simpleDateFormatDefine the date/time format with aSimpleDateFormat.Note: Setting this property makes the exposed ObjectMappernon-thread-safe, according to Jackson's thread safety rules.- See Also:
 
- 
localeOverride the defaultLocaleto use for formatting. Default value used isLocale.getDefault().- Since:
- 4.1.5
 
- 
localeOverride the defaultLocaleto use for formatting. Default value used isLocale.getDefault().- Parameters:
- localeString- the locale ID as a String representation
- Since:
- 4.1.5
 
- 
timeZoneOverride the defaultTimeZoneto use for formatting. Default value used is UTC (NOT local timezone).- Since:
- 4.1.5
 
- 
timeZoneOverride the defaultTimeZoneto use for formatting. Default value used is UTC (NOT local timezone).- Parameters:
- timeZoneString- the zone ID as a String representation
- Since:
- 4.1.5
 
- 
annotationIntrospectorpublic Jackson2ObjectMapperBuilder annotationIntrospector(AnnotationIntrospector annotationIntrospector) Set anAnnotationIntrospectorfor both serialization and deserialization.
- 
annotationIntrospectorpublic Jackson2ObjectMapperBuilder annotationIntrospector(Function<AnnotationIntrospector, AnnotationIntrospector> pairingFunction) Alternative toannotationIntrospector(AnnotationIntrospector)that allows combining with rather than replacing the currently set introspector, e.g. viaAnnotationIntrospector.pair(AnnotationIntrospector, AnnotationIntrospector).- Parameters:
- pairingFunction- a function to apply to the currently set introspector (possibly- null); the result of the function becomes the new introspector.
- Since:
- 5.2.4
 
- 
propertyNamingStrategypublic Jackson2ObjectMapperBuilder propertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) Specify aPropertyNamingStrategyto configure theObjectMapperwith.
- 
defaultTypingSpecify aTypeResolverBuilderto use for Jackson's default typing.- Since:
- 4.2.2
 
- 
serializationInclusionpublic Jackson2ObjectMapperBuilder serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include inclusion) Set a custom inclusion strategy for serialization.- See Also:
- 
- JsonInclude.Include
 
 
- 
serializationInclusionpublic Jackson2ObjectMapperBuilder serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Value serializationInclusion) Set a custom inclusion strategy for serialization.- Since:
- 5.3
- See Also:
- 
- JsonInclude.Value
 
 
- 
filtersSet the global filters to use in order to support@JsonFilterannotated POJO.- Since:
- 4.2
- See Also:
 
- 
mixInAdd mix-in annotations to use for augmenting specified class or interface.- Parameters:
- target- class (or interface) whose annotations to effectively override
- mixinSource- class (or interface) whose annotations are to be "added" to target's annotations as value
- Since:
- 4.1.2
- See Also:
 
- 
mixInsAdd mix-in annotations to use for augmenting specified class or interface.- Parameters:
- mixIns- a Map of entries with target classes (or interface) whose annotations to effectively override as key and mix-in classes (or interface) whose annotations are to be "added" to target's annotations as value.
- Since:
- 4.1.2
- See Also:
 
- 
serializersConfigure custom serializers. Each serializer is registered for the type returned byJsonSerializer.handledType(), which must not benull.- See Also:
 
- 
serializerByTypeConfigure a custom serializer for the given type.- Since:
- 4.1.2
- See Also:
 
- 
serializersByTypeConfigure custom serializers for the given types.- See Also:
 
- 
deserializersConfigure custom deserializers. Each deserializer is registered for the type returned byJsonDeserializer.handledType(), which must not benull.- Since:
- 4.3
- See Also:
 
- 
deserializerByTypepublic Jackson2ObjectMapperBuilder deserializerByType(Class<?> type, JsonDeserializer<?> deserializer) Configure a custom deserializer for the given type.- Since:
- 4.1.2
 
- 
deserializersByTypepublic Jackson2ObjectMapperBuilder deserializersByType(Map<Class<?>, JsonDeserializer<?>> deserializers) Configure custom deserializers for the given types.
- 
autoDetectFieldsShortcut forMapperFeature.AUTO_DETECT_FIELDSoption.
- 
autoDetectGettersSetters
- 
defaultViewInclusionShortcut forMapperFeature.DEFAULT_VIEW_INCLUSIONoption.
- 
failOnUnknownPropertiesShortcut forDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESoption.
- 
failOnEmptyBeansShortcut forSerializationFeature.FAIL_ON_EMPTY_BEANSoption.
- 
indentOutputShortcut forSerializationFeature.INDENT_OUTPUToption.
- 
defaultUseWrapperDefine if a wrapper will be used for indexed (List, array) properties or not by default (only applies toXmlMapper).- Since:
- 4.3
 
- 
visibilitypublic Jackson2ObjectMapperBuilder visibility(com.fasterxml.jackson.annotation.PropertyAccessor accessor, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility visibility) Specify visibility to limit what kind of properties are auto-detected.- Since:
- 5.1
- See Also:
- 
- PropertyAccessor
- JsonAutoDetect.Visibility
 
 
- 
featuresToEnableSpecify features to enable.
- 
featuresToDisableSpecify features to disable.
- 
modulesSpecify the modules to be registered with theObjectMapper.Multiple invocations are not additive, the last one defines the modules to register. Note: If this is set, no finding of modules is going to happen - not by Jackson, and not by Spring either (see findModulesViaServiceLoader). As a consequence, specifying an empty list here will suppress any kind of module detection.Specify either this or modulesToInstall(com.fasterxml.jackson.databind.Module...), not both.- Since:
- 4.1.5
- See Also:
 
- 
modulesVariant ofmodules(Module...)with aList.- See Also:
 
- 
modulesVariant ofmodules(Module...)with aConsumerfor full control over the underlying list of modules.- Since:
- 5.3.22
- See Also:
 
- 
modulesToInstallSpecify one or more modules to be registered with theObjectMapper.Multiple invocations are not additive, the last one defines the modules to register. Modules specified here will be registered after Spring's autodetection of JSR-310 and Joda-Time, or Jackson's finding of modules (see findModulesViaServiceLoader), allowing to eventually override their configuration.Specify either this or modules(Module...), not both.- Since:
- 4.1.5
- See Also:
 
- 
modulesToInstallVariant ofmodulesToInstall(Module...)with aConsumerfor full control over the underlying list of modules.- Since:
- 5.3.22
- See Also:
 
- 
modulesToInstall@SafeVarargs public final Jackson2ObjectMapperBuilder modulesToInstall(Class<? extends Module>... modules) Specify one or more modules by class to be registered with theObjectMapper.Multiple invocations are not additive, the last one defines the modules to register. Modules specified here will be registered after Spring's autodetection of JSR-310 and Joda-Time, or Jackson's finding of modules (see findModulesViaServiceLoader), allowing to eventually override their configuration.Specify either this or modules(Module...), not both.
- 
findModulesViaServiceLoaderSet whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath.If this mode is not set, Spring's Jackson2ObjectMapperBuilder 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 Also:
 
- 
moduleClassLoaderSet the ClassLoader to use for loading Jackson extension modules.
- 
handlerInstantiatorCustomize the construction of Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).- Since:
- 4.1.3
- See Also:
 
- 
applicationContextSet the SpringApplicationContextin order to autowire Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).- Since:
- 4.1.3
- See Also:
 
- 
postConfigurerAn option to apply additional customizations directly to theObjectMapperinstances at the end, after all other config properties of the builder have been applied.- Parameters:
- configurer- a configurer to apply. If several configurers are registered, they will get applied in their registration order.
- Since:
- 5.3
 
- 
buildBuild a newObjectMapperinstance.Each build operation produces an independent ObjectMapperinstance. The builder's settings can get modified, with a subsequent build operation then producing a newObjectMapperbased on the most recent settings.- Returns:
- the newly built ObjectMapper
 
- 
configureConfigure an existingObjectMapperinstance with this builder's settings. This can be applied to any number ofObjectMappers.- Parameters:
- objectMapper- the ObjectMapper to configure
 
- 
jsonObtain aJackson2ObjectMapperBuilderinstance in order to build a regular JSONObjectMapperinstance.
- 
xmlObtain aJackson2ObjectMapperBuilderinstance in order to build anXmlMapperinstance.
- 
smileObtain aJackson2ObjectMapperBuilderinstance in order to build a Smile data formatObjectMapperinstance.- Since:
- 5.0
 
- 
cborObtain aJackson2ObjectMapperBuilderinstance in order to build a CBOR data formatObjectMapperinstance.- Since:
- 5.0
 
 
-