{"record":{"id":"07e9c27ba675e874","repo":"baomidou/mybatis-plus","slug":"failed-to-serialize-object-of-type","errorCode":null,"errorMessage":"Failed to serialize object of type: {}","messagePattern":"Failed to serialize object of type: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/SerializationUtils.java","lineNumber":64,"sourceCode":"        return (T) deserialize(objectData);\n    }\n\n    /**\n     * Serialize the given object to a byte array.\n     *\n     * @param object the object to serialize\n     * @return an array of bytes representing the object in a portable fashion\n     */\n    public static byte[] serialize(Object object) {\n        if (object == null) {\n            return null;\n        }\n        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);\n        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {\n            oos.writeObject(object);\n            oos.flush();\n        } catch (IOException ex) {\n            throw new IllegalArgumentException(\"Failed to serialize object of type: \" + object.getClass(), ex);\n        }\n        return baos.toByteArray();\n    }\n\n    /**\n     * Deserialize the byte array into an object.\n     *\n     * @param bytes a serialized object\n     * @return the result of deserializing the bytes\n     */\n    public static Object deserialize(byte[] bytes) {\n        if (bytes == null) {\n            return null;\n        }\n        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {\n            return ois.readObject();\n        } catch (IOException ex) {\n            throw new IllegalArgumentException(\"Failed to deserialize object\", ex);","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/SerializationUtils.java#L46-L82","documentation":"SerializationUtils.serialize(object) writes the object to an ObjectOutputStream and converts any IOException into IllegalArgumentException('Failed to serialize object of type: <class>'). In practice the almost-universal cause is NotSerializableException (a subclass of IOException): some object in the graph does not implement java.io.Serializable.","triggerScenarios":"Passing an object graph containing a non-Serializable element (a field holding a Connection, InputStream, lambda capturing one, Thread, or a POJO without implements Serializable). mybatis-plus uses this helper for cache puts (second-level cache serialization) and similar plumbing.","commonSituations":"Enabling a serialized second-level cache on mappers whose entities embed non-serializable fields (e.g. java.time fields in old setups, custom types, open streams); caching entities with lazy-loading proxy objects that hold session internals.","solutions":["Make every class in the object graph implement Serializable (including nested types and superclasses).","Mark non-serializable/transient-volatile references as transient and reconstruct them in readResolve/writeReplace if needed.","If a field cannot be serialized, exclude it from the cached form (DTO projection) or switch the cache to a non-serializing store.","Check the wrapped NotSerializableException in the cause chain — it names the exact offending class."],"exampleFix":"// before\npublic class User implements Serializable {\n    private Connection conn; // NotSerializableException inside IOException\n}\n\n// after\npublic class User implements Serializable {\n    private static final long serialVersionUID = 1L;\n    private transient Connection conn;\n}","handlingStrategy":"validation","validationCode":"if (object != null && !(object instanceof java.io.Serializable)\n        && !isPrimitivelySerializable(object)) {\n    throw new IllegalArgumentException(\"object graph must be Serializable: \" + object.getClass());\n}","typeGuard":"static boolean isSerializableDeep(Class<?> c, Set<Class<?>> seen) {\n    if (c == null || c.isPrimitive() || Serializable.class.isAssignableFrom(c)\n            || c == String.class || Number.class.isAssignableFrom(c)) return true;\n    if (!seen.add(c)) return true;\n    for (Field f : c.getDeclaredFields()) {\n        if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())) continue;\n        if (!isSerializableDeep(f.getType(), seen)) return false;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    byte[] bytes = SerializationUtils.serialize(obj);\n} catch (IllegalArgumentException e) {\n    Throwable cause = e.getCause(); // NotSerializableException names the offending class\n    throw new IllegalStateException(\"Non-serializable element: \" + cause, e);\n}","preventionTips":["Implement Serializable with explicit serialVersionUID on all cached entity types.","Mark environment-bound fields (connections, streams) transient.","Add a unit test that round-trips serialize/deserialize every cached type."],"tags":["serialization","cache","java-io"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}