diff --git a/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/StringOrBytesSerializer.java b/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/StringOrBytesSerializer.java index 8dc3f56062..9730073a33 100644 --- a/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/StringOrBytesSerializer.java +++ b/spring-kafka/src/main/java/org/springframework/kafka/support/serializer/StringOrBytesSerializer.java @@ -27,6 +27,7 @@ * Convenient when used with one of the Json message converters. * * @author Gary Russell + * @author Ngoc Nhan * @since 2.3 * */ @@ -42,21 +43,23 @@ public void configure(Map configs, boolean isKey) { @SuppressWarnings("NullAway") // Dataflow analysis limitation @Override public byte[] serialize(String topic, Object data) { - if (data instanceof byte[]) { - return (byte[]) data; - } - else if (data instanceof Bytes) { - return ((Bytes) data).get(); + if (data == null) { + return null; } - else if (data instanceof String) { - return this.stringSerializer.serialize(topic, (String) data); + + if (data instanceof byte[] bytes) { + return bytes; } - else if (data == null) { - return null; + + if (data instanceof Bytes bytes) { + return bytes.get(); } - else { - throw new IllegalStateException("This serializer can only handle byte[], Bytes or String values"); + + if (data instanceof String string) { + return this.stringSerializer.serialize(topic, string); } + + throw new IllegalStateException("This serializer can only handle byte[], Bytes or String values"); } @Override diff --git a/spring-kafka/src/test/java/org/springframework/kafka/support/serializer/StringOrBytesSerializerTests.java b/spring-kafka/src/test/java/org/springframework/kafka/support/serializer/StringOrBytesSerializerTests.java index 2a8a5b28ef..d4c6bf4380 100644 --- a/spring-kafka/src/test/java/org/springframework/kafka/support/serializer/StringOrBytesSerializerTests.java +++ b/spring-kafka/src/test/java/org/springframework/kafka/support/serializer/StringOrBytesSerializerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2024 the original author or authors. + * Copyright 2019-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,10 +26,12 @@ import org.springframework.kafka.test.utils.KafkaTestUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Gary Russell * @author Soby Chacko + * @author Ngoc Nhan * @since 2.3 * */ @@ -51,6 +53,9 @@ void test() { Map configs = Collections.singletonMap("serializer.encoding", "UTF-16"); serializer.configure(configs, false); assertThat(KafkaTestUtils.getPropertyValue(serializer, "stringSerializer.encoding")).isEqualTo(StandardCharsets.UTF_16); + assertThat(serializer.serialize("null", null)).isNull(); + assertThatIllegalStateException().isThrownBy(() -> serializer.serialize("ex", 0)) + .withMessage("This serializer can only handle byte[], Bytes or String values"); } }