Why HSF’s Hessian2 Serialization Fails with Java 9 Immutable Collections (Set.of)
When an HSF service receives a parameter created with Java 9's immutable collection factories such as Set.of(), the default Hessian2 serializer drops the collection elements, causing a NullArray InvalidObjectException and resulting in a SERVER_SERIALIZE_ERROR on the server side, which can be avoided by using mutable collections or a compatible serializer.
Problem
When calling an HSF service with a parameter created by Set.of() (Java 9 immutable collection), the server throws a serialization error.
Error messages
com.taobao.hsf.exception.HSFException: SERVER_SERIALIZE_ERROR
error message : [] Error log: Deserialize request error on provider side. Please make sure the arguments of request are serializable and your dependency is latestRoot cause
HSF uses Hessian2 as the default serializer. Hessian2 supports Java's writeReplace and readResolve methods but does not invoke custom writeObject / readObject. Java 9 immutable collections implement serialization via writeReplace that returns a java.util.CollSer object whose array field is transient. Hessian2 serializes only non‑transient fields, so the array (the actual elements) is omitted. During deserialization CollSer.readResolve() sees a null array and throws InvalidObjectException: null array, which propagates as the HSF error above.
Key classes
java.util.ImmutableCollections$List12(example of an immutable list) java.util.CollSer – the proxy class used by immutable collections WriteReplaceSerializer – HSF serializer that calls
writeReplace UnsafeSerializer– HSF serializer for custom types
Serialization flow
HSF selects a serializer (Hessian2Serializer → Hessian2Output → SerializerFactory).
For a custom object ( TestParam) it writes the class definition, field names, then the field values.
The field idList holds a List.of(1001,2002). Its writeReplace returns a CollSer with tag=1 and a transient array.
Hessian2 writes only tag, dropping the array.
During deserialization, CollSer.readResolve() attempts to rebuild the list from the missing array and throws InvalidObjectException.
Work‑around
Replace immutable collections with mutable ones (e.g., new HashSet<>()) before passing them to HSF, or use a serializer that fully supports Java’s default serialization.
Additional notes
Guava’s ImmutableList/Set/Map and pre‑Java‑9 Collections.unmodifiableXXX() work with HSF Hessian2 because they do not rely on the transient array field.
Test environment: JDK 11, HSF 3.1.29‑fix‑1‑global, Guava 33.0.0‑jre.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
