@@ -47,16 +47,41 @@ public class DelegatingSerializer implements Serializer<Object> {
4747 private static final LogAccessor LOGGER = new LogAccessor (DelegatingDeserializer .class );
4848
4949 /**
50- * Name of the header containing the serialization selector.
50+ * Synonym for {@link #VALUE_SERIALIZATION_SELECTOR}.
51+ * @deprecated in favor of {@link #VALUE_SERIALIZATION_SELECTOR}.
5152 */
53+ @ Deprecated
5254 public static final String SERIALIZATION_SELECTOR = "spring.kafka.serialization.selector" ;
5355
5456 /**
55- * Name of the configuration property containing the serialization selector map with
56- * format {@code selector:class,...}.
57+ * Name of the header containing the serialization selector for values.
5758 */
59+ public static final String VALUE_SERIALIZATION_SELECTOR = "spring.kafka.serialization.selector" ;
60+
61+ /**
62+ * Name of the header containing the serialization selector for keys.
63+ */
64+ public static final String KEY_SERIALIZATION_SELECTOR = "spring.kafka.key.serialization.selector" ;
65+
66+ /**
67+ * Synonym for {@link #VALUE_SERIALIZATION_SELECTOR_CONFIG}.
68+ * @deprecated in favor of {@link #VALUE_SERIALIZATION_SELECTOR_CONFIG}.
69+ */
70+ @ Deprecated
5871 public static final String SERIALIZATION_SELECTOR_CONFIG = "spring.kafka.serialization.selector.config" ;
5972
73+ /**
74+ * Name of the configuration property containing the serialization selector map for
75+ * values with format {@code selector:class,...}.
76+ */
77+ public static final String VALUE_SERIALIZATION_SELECTOR_CONFIG = "spring.kafka.serialization.selector.config" ;
78+
79+ /**
80+ * Name of the configuration property containing the serialization selector map for
81+ * keys with format {@code selector:class,...}.
82+ */
83+ public static final String KEY_SERIALIZATION_SELECTOR_CONFIG = "spring.kafka.key.serialization.selector.config" ;
84+
6085 private final Map <String , Serializer <?>> delegates = new ConcurrentHashMap <>();
6186
6287 private final Map <String , Object > autoConfigs = new HashMap <>();
@@ -65,17 +90,18 @@ public class DelegatingSerializer implements Serializer<Object> {
6590
6691 /**
6792 * Construct an instance that will be configured in {@link #configure(Map, boolean)}
68- * with a producer property
69- * {@link DelegatingSerializer#SERIALIZATION_SELECTOR_CONFIG }.
93+ * with producer properties {@link #VALUE_SERIALIZATION_SELECTOR_CONFIG} and
94+ * {@link #KEY_SERIALIZATION_SELECTOR_CONFIG }.
7095 */
7196 public DelegatingSerializer () {
7297 }
7398
7499 /**
75100 * Construct an instance with the supplied mapping of selectors to delegate
76101 * serializers. The selector must be supplied in the
77- * {@link DelegatingSerializer#SERIALIZATION_SELECTOR} header. It is not necessary to
78- * configure standard serializers supported by {@link Serdes}.
102+ * {@link #KEY_SERIALIZATION_SELECTOR} and/or {@link #VALUE_SERIALIZATION_SELECTOR}
103+ * headers. It is not necessary to configure standard serializers supported by
104+ * {@link Serdes}.
79105 * @param delegates the map of delegates.
80106 */
81107 public DelegatingSerializer (Map <String , Serializer <?>> delegates ) {
@@ -87,7 +113,8 @@ public DelegatingSerializer(Map<String, Serializer<?>> delegates) {
87113 public void configure (Map <String , ?> configs , boolean isKey ) {
88114 this .autoConfigs .putAll (configs );
89115 this .forKeys = isKey ;
90- Object value = configs .get (SERIALIZATION_SELECTOR_CONFIG );
116+ String configKey = configKey ();
117+ Object value = configs .get (configKey );
91118 if (value == null ) {
92119 return ;
93120 }
@@ -103,7 +130,7 @@ else if (serializer instanceof String) {
103130 createInstanceAndConfigure (configs , isKey , this .delegates , selector , (String ) serializer );
104131 }
105132 else {
106- throw new IllegalStateException (SERIALIZATION_SELECTOR_CONFIG
133+ throw new IllegalStateException (configKey
107134 + " map entries must be Serializers or class names, not " + value .getClass ());
108135 }
109136 });
@@ -113,10 +140,14 @@ else if (value instanceof String) {
113140 }
114141 else {
115142 throw new IllegalStateException (
116- SERIALIZATION_SELECTOR_CONFIG + " must be a map or String, not " + value .getClass ());
143+ configKey + " must be a map or String, not " + value .getClass ());
117144 }
118145 }
119146
147+ private String configKey () {
148+ return this .forKeys ? KEY_SERIALIZATION_SELECTOR_CONFIG : VALUE_SERIALIZATION_SELECTOR_CONFIG ;
149+ }
150+
120151 protected static Map <String , Serializer <?>> createDelegates (String mappings , Map <String , ?> configs ,
121152 boolean isKey ) {
122153
@@ -173,19 +204,20 @@ public byte[] serialize(String topic, Object data) {
173204 @ Override
174205 public byte [] serialize (String topic , Headers headers , Object data ) {
175206 byte [] value = null ;
176- Header header = headers .lastHeader (SERIALIZATION_SELECTOR );
207+ String selectorKey = selectorKey ();
208+ Header header = headers .lastHeader (selectorKey );
177209 if (header != null ) {
178210 value = header .value ();
179211 }
180212 if (value == null ) {
181213 value = trySerdes (data );
182214 if (value == null ) {
183- throw new IllegalStateException ("No '" + SERIALIZATION_SELECTOR
215+ throw new IllegalStateException ("No '" + selectorKey
184216 + "' header present and type (" + data .getClass ().getName ()
185217 + ") is not supported by Serdes" );
186218 }
187219 try {
188- headers .add (new RecordHeader (SERIALIZATION_SELECTOR , value ));
220+ headers .add (new RecordHeader (selectorKey , value ));
189221 }
190222 catch (IllegalStateException e ) {
191223 LOGGER .debug (e , () -> "Could not set header for type " + data .getClass ());
@@ -196,11 +228,15 @@ public byte[] serialize(String topic, Headers headers, Object data) {
196228 Serializer <Object > serializer = (Serializer <Object >) this .delegates .get (selector );
197229 if (serializer == null ) {
198230 throw new IllegalStateException (
199- "No serializer found for '" + SERIALIZATION_SELECTOR + "' header with value '" + selector + "'" );
231+ "No serializer found for '" + selectorKey + "' header with value '" + selector + "'" );
200232 }
201233 return serializer .serialize (topic , headers , data );
202234 }
203235
236+ private String selectorKey () {
237+ return this .forKeys ? KEY_SERIALIZATION_SELECTOR : VALUE_SERIALIZATION_SELECTOR ;
238+ }
239+
204240 /*
205241 * Package for testing.
206242 */
0 commit comments