Class JacksonSerializer.Builder

java.lang.Object
org.axonframework.serialization.json.JacksonSerializer.Builder
Enclosing class:
JacksonSerializer

public static class JacksonSerializer.Builder extends Object
Builder class to instantiate a JacksonSerializer.

The RevisionResolver is defaulted to an AnnotationRevisionResolver, the Converter to a ChainingConverter, the ObjectMapper defaults to a ObjectMapper() result and the ClassLoader to the ClassLoader of this class.

Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer and the (2) JavaTimeModule. Lastly, if the provided converter is of type ChainingConverter, the JacksonSerializer.registerConverters(org.axonframework.serialization.ChainingConverter) is performed to automatically add the JsonNodeToByteArrayConverter and ByteArrayToJsonNodeConverter.

  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • revisionResolver

      public JacksonSerializer.Builder revisionResolver(RevisionResolver revisionResolver)
      Sets the RevisionResolver used to resolve the revision from an object to be serialized. Defaults to an AnnotationRevisionResolver which resolves the revision based on the contents of the Revision annotation on the serialized classes.
      Parameters:
      revisionResolver - a RevisionResolver used to resolve the revision from an object to be serialized
      Returns:
      the current Builder instance, for fluent interfacing
    • converter

      public JacksonSerializer.Builder converter(Converter converter)
      Sets the Converter used as a converter factory providing converter instances utilized by upcasters to convert between different content types. Defaults to a ChainingConverter.
      Parameters:
      converter - a Converter used as a converter factory providing converter instances utilized by upcasters to convert between different content types
      Returns:
      the current Builder instance, for fluent interfacing
    • objectMapper

      public JacksonSerializer.Builder objectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Sets the ObjectMapper used to serialize and parse the objects to JSON. This ObjectMapper allows for customization of the serialized form. Defaults to the output of ObjectMapper().
      Parameters:
      objectMapper - an ObjectMapper used to serialize and parse the objects to JSON
      Returns:
      the current Builder instance, for fluent interfacing
    • classLoader

      public JacksonSerializer.Builder classLoader(ClassLoader classLoader)
      Sets the ClassLoader used as an override for default ClassLoader used in the ObjectMapper. The same solution could thus be achieved by configuring the `ObjectMapper` instance directly.
      Parameters:
      classLoader - the ClassLoader used to load classes with when deserializing
      Returns:
      the current Builder instance, for fluent interfacing
      See Also:
    • lenientDeserialization

      public JacksonSerializer.Builder lenientDeserialization()
      Configures the underlying ObjectMapper to be lenient when deserializing JSON into Java objects. Specifically, enables the DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY and DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, and disables DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.
      Returns:
      the current Builder instance, for fluent interfacing
    • disableCachingOfUnknownClasses

      public JacksonSerializer.Builder disableCachingOfUnknownClasses()
      Disables the caching of the names for classes which couldn't be resolved to a class.

      It is recommended to disable this in case the class loading is dynamic, and classes that are not available at one point in time, may become available at any time later.

      Returns:
      the current Builder instance, for fluent interfacing
    • defaultTyping

      public JacksonSerializer.Builder defaultTyping()
      Configures the underlying ObjectMapper to include type information when serializing Java objects into JSON. Specifically, it calls ObjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping) method, using ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS. This can be toggled on to allow Collections of objects, for example query List responses, to automatically include the types without require the use of JsonTypeInfo on the objects themselves.
      Returns:
      the current Builder instance, for fluent interfacing
    • build

      public JacksonSerializer build()
      Initializes a JacksonSerializer as specified through this Builder.
      Returns:
      a JacksonSerializer as specified through this Builder
    • validate

      protected void validate() throws AxonConfigurationException
      Validates whether the fields contained in this Builder are set accordingly.
      Throws:
      AxonConfigurationException - if one field is asserted to be incorrect according to the Builder's specifications