{"record":{"id":"e3ad4df6229669e2","repo":"apache/dubbo","slug":"gson-is-not-supported-please-import-gson-in-jvm-e","errorCode":null,"errorMessage":"Gson is not supported. Please import Gson in JVM env.","messagePattern":"Gson is not supported\\. Please import Gson in JVM env\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java","lineNumber":53,"sourceCode":"    private static boolean isSupportGson() {\n        if (supportGson == null) {\n            synchronized (GsonUtils.class) {\n                if (supportGson == null) {\n                    try {\n                        Class<?> aClass = ClassUtils.forName(\"com.google.gson.Gson\");\n                        supportGson = aClass != null;\n                    } catch (Throwable t) {\n                        supportGson = false;\n                    }\n                }\n            }\n        }\n        return supportGson;\n    }\n\n    public static Object fromJson(String json, Type originType) throws RuntimeException {\n        if (!isSupportGson()) {\n            throw new RuntimeException(\"Gson is not supported. Please import Gson in JVM env.\");\n        }\n        Type type = TypeToken.get(originType).getType();\n        try {\n            return getGson().fromJson(json, type);\n        } catch (JsonSyntaxException ex) {\n            throw new RuntimeException(String.format(\n                    \"Generic serialization [%s] Json syntax exception thrown when parsing (message:%s type:%s) error:%s\",\n                    GENERIC_SERIALIZATION_GSON, json, type.toString(), ex.getMessage()));\n        }\n    }\n\n    public static String toJson(Object obj) throws RuntimeException {\n        if (!isSupportGson()) {\n            throw new RuntimeException(\"Gson is not supported. Please import Gson in JVM env.\");\n        }\n        try {\n            return getGson().toJson(obj);\n        } catch (JsonSyntaxException ex) {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GsonUtils.java#L35-L71","documentation":"Thrown by GsonUtils.fromJson(String, Type) when isSupportGson() returns false — i.e. the class com.google.gson.Gson could not be loaded via ClassUtils.forName on the runtime classpath. GsonUtils is a thin static facade over Gson used by Dubbo's generic invocation (generic serialization = gson). Unlike the JSON-parsing exceptions, this is a missing-dependency error: Gson (com.google.gson:gson) is absent from the JVM env.","triggerScenarios":"Calling GsonUtils.fromJson(json, type) on a runtime where the gson jar is not present. isSupportGson() is cached after first check; once it resolves to false it stays false for the JVM lifetime (only resettable via the @Deprecated setSupportGson test hook).","commonSituations":"Slimmed Dubbo distribution or shaded uber-jar that excluded the gson dependency; classloader isolation (OSGi, Tomcat, custom plugin loader) where Gson is on a parent/peer loader Dubbo cannot reach via forName; using Dubbo generic invocation with serialization=gson without declaring the gson artifact; partial upgrade where an old gson was shaded out.","solutions":["Add the Gson dependency to your build/runtime, e.g. Maven: <dependency><groupId>com.google.gson</groupId><artifactId>gson</artifactId></dependency> matching the version Dubbo expects.","If you shaded/excluded gson intentionally, switch the generic serialization away from gson (use a different generic serialization).","Verify the gson jar is actually on the application classloader at runtime (not just compile scope) — check for provided/optional scoping that drops it at runtime.","In a multi-classloader env, ensure Gson loads from the same loader Dubbo uses for ClassUtils.forName."],"exampleFix":"// pom.xml before: gson excluded\n// after: add the dependency\n<dependency>\n  <groupId>com.google.gson</groupId>\n  <artifactId>gson</artifactId>\n  <version>2.10.1</version>\n</dependency>","handlingStrategy":"try-catch","validationCode":"// Fail fast at startup by probing Gson availability via the same path Dubbo uses.\nboolean gsonPresent;\ntry {\n    Class.forName(\"com.google.gson.Gson\");\n    gsonPresent = true;\n} catch (ClassNotFoundException e) {\n    gsonPresent = false;\n}\nif (!gsonPresent) {\n    throw new IllegalStateException(\"gson dependency missing; add com.google.gson:gson to the runtime classpath\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return GsonUtils.fromJson(json, type);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Gson is not supported\")) {\n        throw new IllegalStateException(\"gson dependency missing on classpath\", e);\n    }\n    throw e;\n}","preventionTips":["Declare the gson dependency at runtime scope, not optional/provided, when using serialization=gson.","In multi-classloader environments, ensure Gson loads from the same loader Dubbo's ClassUtils.forName uses.","Probe Gson availability at startup and fail fast with an actionable message.","If Gson is intentionally excluded, switch the generic serialization to a different strategy."],"tags":["json","gson","missing-dependency","generic-invocation","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}