Class JacksonJsonDeserializer<T>

java.lang.Object
org.springframework.kafka.support.serializer.JacksonJsonDeserializer<T>
Type Parameters:
T - class of the entity, representing messages
All Implemented Interfaces:
Closeable, AutoCloseable, org.apache.kafka.common.serialization.Deserializer<T>

public class JacksonJsonDeserializer<T> extends Object implements org.apache.kafka.common.serialization.Deserializer<T>
Generic Deserializer for receiving JSON from Kafka and return Java objects. Based on Jackson 3.

IMPORTANT: Configuration must be done completely with property setters or via configure(Map, boolean), not a mixture. If any setters have been called, configure(Map, boolean) will be a no-op.

Since:
4.0
Author:
Igor Stepanov, Artem Bilan, Gary Russell, Yanming Zhou, Elliot Kennedy, Torsten Schleede, Ivan Ponomarev, Omer Celik, Soby Chacko
  • Field Details

  • Constructor Details

    • JacksonJsonDeserializer

      public JacksonJsonDeserializer()
      Construct an instance with a default JsonMapper.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(tools.jackson.databind.json.JsonMapper jsonMapper)
      Construct an instance with the provided JsonMapper.
      Parameters:
      jsonMapper - a custom object mapper.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable Class<? super T> targetType)
      Construct an instance with the provided target type, and a default JsonMapper.
      Parameters:
      targetType - the target type to use if no type info headers are present.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.core.type.TypeReference<? super T> targetType)
      Construct an instance with the provided target type, and a default JsonMapper.
      Parameters:
      targetType - the target type reference to use if no type info headers are present.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.databind.JavaType targetType)
      Construct an instance with the provided target type, and a default JsonMapper.
      Parameters:
      targetType - the target java type to use if no type info headers are present.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable Class<? super T> targetType, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, and useHeadersIfPresent with a default JsonMapper.
      Parameters:
      targetType - the target type.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.core.type.TypeReference<? super T> targetType, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, and useHeadersIfPresent with a default JsonMapper.
      Parameters:
      targetType - the target type reference.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.databind.JavaType targetType, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, and useHeadersIfPresent with a default JsonMapper.
      Parameters:
      targetType - the target java type.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(Class<? super T> targetType, tools.jackson.databind.json.JsonMapper jsonMapper)
      Construct an instance with the provided target type, and JsonMapper.
      Parameters:
      targetType - the target type to use if no type info headers are present.
      jsonMapper - the mapper. type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(tools.jackson.core.type.TypeReference<? super T> targetType, tools.jackson.databind.json.JsonMapper jsonMapper)
      Construct an instance with the provided target type, and JsonMapper.
      Parameters:
      targetType - the target type reference to use if no type info headers are present.
      jsonMapper - the mapper. type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.databind.JavaType targetType, tools.jackson.databind.json.JsonMapper jsonMapper)
      Construct an instance with the provided target type, and JsonMapper.
      Parameters:
      targetType - the target java type to use if no type info headers are present.
      jsonMapper - the mapper. type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable Class<? super T> targetType, tools.jackson.databind.json.JsonMapper jsonMapper, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, JsonMapper and useHeadersIfPresent.
      Parameters:
      targetType - the target type.
      jsonMapper - the mapper.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.core.type.TypeReference<? super T> targetType, tools.jackson.databind.json.JsonMapper jsonMapper, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, JsonMapper and useHeadersIfPresent.
      Parameters:
      targetType - the target type reference.
      jsonMapper - the mapper.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
    • JacksonJsonDeserializer

      public JacksonJsonDeserializer(@Nullable tools.jackson.databind.JavaType targetType, tools.jackson.databind.json.JsonMapper jsonMapper, boolean useHeadersIfPresent)
      Construct an instance with the provided target type, JsonMapper and useHeadersIfPresent.
      Parameters:
      targetType - the target type reference.
      jsonMapper - the mapper.
      useHeadersIfPresent - true to use headers if present and fall back to target type if not.
  • Method Details

    • getTypeMapper

      public JacksonJavaTypeMapper getTypeMapper()
    • setTypeMapper

      public void setTypeMapper(JacksonJavaTypeMapper typeMapper)
      Set a customized type mapper. If the mapper is a JacksonJavaTypeMapper, any class mappings configured in the mapper will be added to the trusted packages.
      Parameters:
      typeMapper - the type mapper.
    • setUseTypeMapperForKey

      public void setUseTypeMapperForKey(boolean isKey)
      Configure the default JacksonJavaTypeMapper to use key type headers.
      Parameters:
      isKey - Use key type headers if true
    • setRemoveTypeHeaders

      public void setRemoveTypeHeaders(boolean removeTypeHeaders)
      Set to false to retain type information headers after deserialization. Default true.
      Parameters:
      removeTypeHeaders - true to remove headers.
    • setUseTypeHeaders

      public void setUseTypeHeaders(boolean useTypeHeaders)
      Set to false to ignore type information in headers and use the configured target type instead. Only applies if the preconfigured type mapper is used. Default true.
      Parameters:
      useTypeHeaders - false to ignore type headers.
    • setTypeFunction

      public void setTypeFunction(BiFunction<byte[], org.apache.kafka.common.header.Headers, tools.jackson.databind.JavaType> typeFunction)
      Set a BiFunction that receives the data to be deserialized and the headers and returns a JavaType.
      Parameters:
      typeFunction - the function.
    • setTypeResolver

      public void setTypeResolver(JacksonJsonTypeResolver typeResolver)
      Set a JacksonJsonTypeResolver that receives the data to be deserialized and the headers and returns a JavaType.
      Parameters:
      typeResolver - the resolver.
    • configure

      public void configure(Map<String,?> configs, boolean isKey)
      Specified by:
      configure in interface org.apache.kafka.common.serialization.Deserializer<T>
    • addTrustedPackages

      public void addTrustedPackages(String... packages)
      Add trusted packages for deserialization.
      Parameters:
      packages - the packages.
    • deserialize

      public @Nullable T deserialize(String topic, org.apache.kafka.common.header.Headers headers, byte[] data)
      Specified by:
      deserialize in interface org.apache.kafka.common.serialization.Deserializer<T>
    • deserialize

      public @Nullable T deserialize(String topic, byte[] data)
      Specified by:
      deserialize in interface org.apache.kafka.common.serialization.Deserializer<T>
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface org.apache.kafka.common.serialization.Deserializer<T>
    • copyWithType

      public <X> JacksonJsonDeserializer<X> copyWithType(Class<? super X> newTargetType)
      Copies this deserializer with same configuration, except new target type is used.
      Type Parameters:
      X - new deserialization result type
      Parameters:
      newTargetType - type used for when type headers are missing, not null
      Returns:
      new instance of deserializer with type changes
    • copyWithType

      public <X> JacksonJsonDeserializer<X> copyWithType(tools.jackson.core.type.TypeReference<? super X> newTargetType)
      Copies this deserializer with same configuration, except new target type reference is used.
      Type Parameters:
      X - new deserialization result type
      Parameters:
      newTargetType - type reference used for when type headers are missing, not null
      Returns:
      new instance of deserializer with type changes
    • copyWithType

      public <X> JacksonJsonDeserializer<X> copyWithType(tools.jackson.databind.JavaType newTargetType)
      Copies this deserializer with same configuration, except new target java type is used.
      Type Parameters:
      X - new deserialization result type
      Parameters:
      newTargetType - java type used for when type headers are missing, not null
      Returns:
      new instance of deserializer with type changes
    • forKeys

      public JacksonJsonDeserializer<T> forKeys()
      Designate this deserializer for deserializing keys (default is values); only applies if the default type mapper is used.
      Returns:
      the deserializer.
    • dontRemoveTypeHeaders

      public JacksonJsonDeserializer<T> dontRemoveTypeHeaders()
      Don't remove type information headers.
      Returns:
      the deserializer.
      See Also:
    • ignoreTypeHeaders

      public JacksonJsonDeserializer<T> ignoreTypeHeaders()
      Ignore type information headers and use the configured target class.
      Returns:
      the deserializer.
      See Also:
    • typeMapper

      public JacksonJsonDeserializer<T> typeMapper(JacksonJavaTypeMapper mapper)
      Use the supplied JacksonJavaTypeMapper.
      Parameters:
      mapper - the mapper.
      Returns:
      the deserializer.
      See Also:
    • trustedPackages

      public JacksonJsonDeserializer<T> trustedPackages(String... packages)
      Add trusted packages to the default type mapper.
      Parameters:
      packages - the packages.
      Returns:
      the deserializer.
    • typeFunction

      public JacksonJsonDeserializer<T> typeFunction(BiFunction<byte[], org.apache.kafka.common.header.Headers, tools.jackson.databind.JavaType> typeFunction)
      Set a BiFunction that receives the data to be deserialized and the headers and returns a JavaType.
      Parameters:
      typeFunction - the function.
      Returns:
      the deserializer.
    • typeResolver

      public JacksonJsonDeserializer<T> typeResolver(JacksonJsonTypeResolver resolver)
      Set a JsonTypeResolver that receives the data to be deserialized and the headers and returns a JavaType.
      Parameters:
      resolver - the resolver.
      Returns:
      the deserializer.