{"record":{"id":"4b7d472aaacb07cc","repo":"baomidou/mybatis-plus","slug":"failed-to-deserialize-object","errorCode":null,"errorMessage":"Failed to deserialize object","messagePattern":"Failed to deserialize object","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/SerializationUtils.java","lineNumber":82,"sourceCode":"            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);\n        } catch (ClassNotFoundException ex) {\n            throw new IllegalStateException(\"Failed to deserialize object type\", ex);\n        }\n    }\n}\n","sourceCodeStart":64,"sourceCodeEnd":88,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/SerializationUtils.java#L64-L88","documentation":"SerializationUtils.deserialize(bytes) reads an object back via ObjectInputStream; any IOException (corrupt bytes, truncated stream, invalid stream header, stream class version mismatch surfaced as InvalidClassException) is rethrown as IllegalArgumentException('Failed to deserialize object'). The data is unusable — this is not a classpath problem (that produces the ClassNotFoundException branch).","triggerScenarios":"Feeding deserialize() bytes that are not a valid Java serialization stream: truncated arrays (DB column too short, e.g. VARCHAR vs BLOB), corrupted cache entries after an unclean shutdown, binary-mangled by charset conversion, or data serialized with an incompatible class layout (serialVersionUID or structural change causing InvalidClassException).","commonSituations":"Storing serialized blobs in a TEXT/VARCHAR column so the driver mangles bytes; changing entity class structure (adding/removing fields) without maintaining serialVersionUID; cache files surviving a crash; reading bytes that were never produced by serialize() (e.g. JSON or protobuf payload).","solutions":["Confirm the byte[] actually came from Java serialization — check for the stream header (AC ED 00 05) if reading from storage.","Use a binary column type (BLOB/BYTEA/VARBINARY(max)), not TEXT/VARCHAR, for serialized payloads.","Declare an explicit private static final long serialVersionUID on serialized classes so structural changes do not invalidate old data.","Treat unreadable cached entries as cache misses: delete and regenerate instead of retrying."],"exampleFix":"-- before: serialized bytes stored in TEXT get charset-mangled -> IOException on read\nALTER TABLE doc ADD COLUMN payload TEXT;\n\n-- after: use a binary column\nALTER TABLE doc ADD COLUMN payload BLOB;","handlingStrategy":"fallback","validationCode":"if (bytes == null || bytes.length < 4\n        || (bytes[0] & 0xFF) != 0xAC || (bytes[1] & 0xFF) != 0xED) {\n    // not a Java serialization stream; treat as corrupt/cache miss\n}","typeGuard":"static boolean looksLikeJavaSerialized(byte[] b) {\n    return b != null && b.length > 4\n        && (b[0] & 0xFF) == 0xAC && (b[1] & 0xFF) == 0xED\n        && (b[2] & 0xFF) == 0x00 && (b[3] & 0xFF) == 0x05;\n}","tryCatchPattern":"try {\n    return SerializationUtils.deserialize(bytes);\n} catch (IllegalArgumentException e) {\n    log.warn(\"corrupt serialized entry, evicting and recomputing\", e);\n    cache.remove(key);\n    return recompute(key); // cache-miss style fallback\n}","preventionTips":["Store serialized payloads in binary columns only (BLOB/VARBINARY), never TEXT.","Evict unreadable cache entries rather than retrying them.","Pin serialVersionUID on serialized classes."],"tags":["serialization","cache","data-corruption","java-io"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}