Class JacksonJsonSerializer<T>

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

public class JacksonJsonSerializer<T> extends Object implements org.apache.kafka.common.serialization.Serializer<T>
Generic Serializer for sending Java objects to Kafka as JSON. 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, Elliot Kennedy, Wang Zhiyang, Omer Celik, Soby Chacko
  • Field Details

    • ADD_TYPE_INFO_HEADERS

      public static final String ADD_TYPE_INFO_HEADERS
      Kafka config property for disabling adding type headers.
      See Also:
    • TYPE_MAPPINGS

      public static final String TYPE_MAPPINGS
      Kafka config property to add type mappings to the type mapper: 'foo:com.Foo,bar:com.Bar'.
      See Also:
    • jsonMapper

      protected final tools.jackson.databind.json.JsonMapper jsonMapper
    • addTypeInfo

      protected boolean addTypeInfo
    • typeMapper

      protected JacksonJavaTypeMapper typeMapper
  • Constructor Details

    • JacksonJsonSerializer

      public JacksonJsonSerializer()
    • JacksonJsonSerializer

      public JacksonJsonSerializer(tools.jackson.core.type.TypeReference<? super T> targetType)
    • JacksonJsonSerializer

      public JacksonJsonSerializer(tools.jackson.databind.json.JsonMapper jsonMapper)
    • JacksonJsonSerializer

      public JacksonJsonSerializer(tools.jackson.core.type.TypeReference<? super T> targetType, tools.jackson.databind.json.JsonMapper jsonMapper)
    • JacksonJsonSerializer

      public JacksonJsonSerializer(@Nullable tools.jackson.databind.JavaType targetType, tools.jackson.databind.json.JsonMapper jsonMapper)
  • Method Details

    • isAddTypeInfo

      public boolean isAddTypeInfo()
    • setAddTypeInfo

      public void setAddTypeInfo(boolean addTypeInfo)
      Set to false to disable adding type info headers.
      Parameters:
      addTypeInfo - true to add headers.
      Since:
      2.1
    • getTypeMapper

      public JacksonJavaTypeMapper getTypeMapper()
    • setTypeMapper

      public void setTypeMapper(JacksonJavaTypeMapper typeMapper)
      Set a customized type mapper.
      Parameters:
      typeMapper - the type mapper.
      Since:
      2.1
    • setUseTypeMapperForKey

      public void setUseTypeMapperForKey(boolean isKey)
      Configure the default Jackson2JavaTypeMapper to use key type headers.
      Parameters:
      isKey - Use key type headers if true
      Since:
      2.1.3
    • configure

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

      protected static Map<String,Class<?>> createMappings(String mappings)
    • serialize

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

      public byte[] serialize(String topic, @Nullable T data)
      Specified by:
      serialize in interface org.apache.kafka.common.serialization.Serializer<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.Serializer<T>
    • copyWithType

      public <X> JacksonJsonSerializer<X> copyWithType(Class<? super X> newTargetType)
      Copies this serializer with same configuration, except new target type reference is used.
      Type Parameters:
      X - new serialization source type
      Parameters:
      newTargetType - type reference forced for serialization, not null
      Returns:
      new instance of serializer with type changes
      Since:
      2.6
    • copyWithType

      public <X> JacksonJsonSerializer<X> copyWithType(tools.jackson.core.type.TypeReference<? super X> newTargetType)
      Copies this serializer with same configuration, except new target type reference is used.
      Type Parameters:
      X - new serialization source type
      Parameters:
      newTargetType - type reference forced for serialization, not null
      Returns:
      new instance of serializer with type changes
      Since:
      2.6
    • copyWithType

      public <X> JacksonJsonSerializer<X> copyWithType(tools.jackson.databind.JavaType newTargetType)
      Copies this serializer with same configuration, except new target java type is used.
      Type Parameters:
      X - new serialization source type
      Parameters:
      newTargetType - java type forced for serialization, not null
      Returns:
      new instance of serializer with type changes
      Since:
      2.6
    • forKeys

      public JacksonJsonSerializer<T> forKeys()
      Designate this serializer for serializing keys (default is values); only applies if the default type mapper is used.
      Returns:
      the serializer.
      Since:
      2.3
      See Also:
    • noTypeInfo

      public JacksonJsonSerializer<T> noTypeInfo()
      Do not include type info headers.
      Returns:
      the serializer.
      Since:
      2.3
      See Also:
    • typeMapper

      public JacksonJsonSerializer<T> typeMapper(JacksonJavaTypeMapper mapper)
      Use the supplied JacksonJavaTypeMapper.
      Parameters:
      mapper - the mapper.
      Returns:
      the serializer.
      Since:
      2.3
      See Also: