Class DelegatingByTypeSerializer

java.lang.Object
org.springframework.kafka.support.serializer.DelegatingByTypeSerializer
All Implemented Interfaces:
Closeable, AutoCloseable, org.apache.kafka.common.serialization.Serializer<Object>

public class DelegatingByTypeSerializer extends Object implements org.apache.kafka.common.serialization.Serializer<Object>
Delegates to a serializer based on type.
Since:
2.7.9
Author:
Gary Russell, Artem Bilan, Wang Zhiyang, Mahesh Aravind V
  • Constructor Summary

    Constructors
    Constructor
    Description
    DelegatingByTypeSerializer(Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates)
    Construct an instance with the map of delegates; keys matched exactly.
    DelegatingByTypeSerializer(Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates, boolean assignable)
    Construct an instance with the map of delegates.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    configure(Map<String,?> configs, boolean isKey)
     
    protected <T> org.apache.kafka.common.serialization.Serializer<T>
    findDelegate(T data)
     
    protected <T> org.apache.kafka.common.serialization.Serializer<T>
    findDelegate(T data, Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates)
    Determine the serializer for the data type.
    protected boolean
    Returns true if findDelegate(Object, Map) should consider assignability to the key rather than an exact match.
    byte[]
    serialize(String topic, Object data)
     
    byte[]
    serialize(String topic, org.apache.kafka.common.header.Headers headers, Object data)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.apache.kafka.common.serialization.Serializer

    close
  • Constructor Details

    • DelegatingByTypeSerializer

      public DelegatingByTypeSerializer(Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates)
      Construct an instance with the map of delegates; keys matched exactly.
      Parameters:
      delegates - the delegates.
    • DelegatingByTypeSerializer

      public DelegatingByTypeSerializer(Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates, boolean assignable)
      Construct an instance with the map of delegates. If assignable is false, only exact key matches are considered. If assignable is true, a delegate is selected if its key class is assignable from the target object's class. When multiple matches are possible, the most specific matching class is selected — that is, the closest match in the class hierarchy.
      Parameters:
      delegates - the delegates
      assignable - true if findDelegate(Object, Map) should consider assignability to the key rather than an exact match.
      Since:
      2.8.3
  • Method Details

    • isAssignable

      protected boolean isAssignable()
      Returns true if findDelegate(Object, Map) should consider assignability to the key rather than an exact match.
      Returns:
      true if assignable.
      Since:
      2.8.3
    • configure

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

      public byte[] serialize(String topic, Object data)
      Specified by:
      serialize in interface org.apache.kafka.common.serialization.Serializer<Object>
    • serialize

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

      protected <T> org.apache.kafka.common.serialization.Serializer<T> findDelegate(T data)
    • findDelegate

      protected <T> org.apache.kafka.common.serialization.Serializer<T> findDelegate(T data, Map<Class<?>,org.apache.kafka.common.serialization.Serializer<?>> delegates)
      Determine the serializer for the data type.
      Type Parameters:
      T - the data type
      Parameters:
      data - the data.
      delegates - the available delegates.
      Returns:
      the delegate.
      Throws:
      org.apache.kafka.common.errors.SerializationException - when there is no match.
      Since:
      2.8.3