While upgrading some code for the upcoming Spring 6 Recipes I noticed an error when migrating a class to a record.
Exception in thread "main" org.springframework.data.redis.serializer.SerializationException: Cannot serialize
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:96)
at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:128)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
at com.apress.spring6recipes.nosql.Main.main(Main.java:16)
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:64)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:33)
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:94)
... 3 more
Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.apress.spring6recipes.nosql.Vehicle]
at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43)
at org.springframework.core.serializer.Serializer.serializeToByteArray(Serializer.java:56)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:60)
... 5 more
Apparently the DefaultSerializer doesn't work with records, whereas calling SerializationUtils.serialize does work (and it uses the same mechanism of writing but doesn't check for Serializable). The check probably needs to be extended to include records as well (or converted to a try/catch instead).
This probably affects Spring 5.3 as well as that is compatible with JDK17 and thus records.
Comment From: sbrannen
While upgrading some code for the upcoming Spring 6 Recipes I noticed an error when migrating a class to a record.
While converting from a class to a record, did you directly or indirectly remove implements Serializable?
The reason I ask is that your class was likely Serializable, and if you make your record Serializable a serializable record should also work.
Comment From: sbrannen
whereas calling
SerializationUtils.serializedoes work
Did you verify that?
The following test fails for me with a cause of java.io.NotSerializableException: org.springframework.util.SerializationUtilsTests$1Person.
@Test
@SuppressWarnings("deprecation")
void serializeRecord() {
record Person(String firstName, String lastName) {}
Person jane = new Person("Jane", "Doe");
assertThat(SerializationUtils.deserialize(SerializationUtils.serialize(jane))).isEqualTo(jane);
}
Whereas, making the record serializable as below allows the above test to pass.
record Person(String firstName, String lastName) implements Serializable {}
Comment From: mdeinum
My bad... Staring at a screen for too long. My regular SerializationUtils.serialize fails as well, but for some reason I didn't notice that. Closing it for a the error being between the chair and keyboard and sorry for the noise.
Comment From: sbrannen
No worries. I was just about to close this issue in light of the passing tests in 5c2870ebd973957dd67405fc747b8ec0eab80667 which demonstrate the expected behavior.