{"record":{"id":"c0801b0e4b72f86d","repo":"alibaba/spring-ai-alibaba","slug":"parametername-value-type","errorCode":null,"errorMessage":"参数 ${parameterName} 的值 ${value} 无法转换为类型 ${type}","messagePattern":"参数 (.+?) 的值 (.+?) 无法转换为类型 (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/dto/ModelConfigInfo.java","lineNumber":75,"sourceCode":"    /**\n     * 获取指定参数值（指定类型）\n     *\n     * @param parameterName 参数名\n     * @param type         期望的类型\n     * @param <T>          类型参数\n     * @return 参数值\n     */\n    @SuppressWarnings(\"unchecked\")\n    public <T> T getParameter(String parameterName, Class<T> type) {\n        Object value = parameters.get(parameterName);\n        if (value == null) {\n            return null;\n        }\n        \n        try {\n            return (T) value;\n        } catch (ClassCastException e) {\n            throw new IllegalArgumentException(\n                String.format(\"参数 %s 的值 %s 无法转换为类型 %s\", \n                    parameterName, value, type.getSimpleName()), e);\n        }\n    }\n\n    /**\n     * 设置参数值\n     *\n     * @param parameterName 参数名\n     * @param value        参数值\n     */\n    public void setParameter(String parameterName, Object value) {\n        parameters.put(parameterName, value);\n    }\n\n    /**\n     * 检查是否包含指定参数\n     *","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/dto/ModelConfigInfo.java#L57-L93","documentation":"ModelConfigInfo.getParameter() retrieves a typed parameter from the model config map and attempts an unchecked cast `(T) value`. If the stored value's runtime type does not match the requested type T, the ClassCastException is caught and rethrown as IllegalArgumentException: \"参数 X 的值 V 无法转换为类型 T\" (\"value of parameter X cannot be converted to type T\"). It is a type-safety guard over the untyped parameter map.","triggerScenarios":"Calling getParameter(\"temperature\", Double.class) when the map holds a String (e.g. \"0.7\" parsed from YAML/JSON without conversion), or getParameter(..., Integer.class) on a Double/Long value stored by a deserializer.","commonSituations":"YAML/JSON config files where numbers were written as quoted strings; different writers storing the same parameter with different numeric types; user-edited model config with wrong value type.","solutions":["Fix the config source so the value has the correct type (unquote numbers in YAML, e.g. temperature: 0.7 not temperature: \"0.7\").","Convert instead of casting: use Number intermediate (`((Number) value).doubleValue()`) or a converter before the cast.","Validate parameter types when loading/parsing the config file rather than at lookup time.","Check which caller passes the wrong expected type T and align it with what is actually stored."],"exampleFix":"// before\nreturn (T) value;\n// after\nif (type == Double.class && value instanceof Number n) { return (T) Double.valueOf(n.doubleValue()); }\nreturn (T) value;","handlingStrategy":"validation","validationCode":"Object v = params.get(name);\nif (v != null && !(type.isInstance(v)) && !(v instanceof Number)) {\n    throw new IllegalArgumentException(\"param \" + name + \" has type \" + v.getClass());\n}","typeGuard":"static <T> boolean isOfType(Object v, Class<T> t) { return v == null || t.isInstance(v) || (Number.class.isAssignableFrom(t) && v instanceof Number); }","tryCatchPattern":"try { return config.getParameter(\"temperature\", Double.class); } catch (IllegalArgumentException e) { log.warn(\"bad param type, using default\", e); return 0.7d; }","preventionTips":["Never quote numeric values in YAML config files","Cast via Number intermediate instead of direct (T) casts for numeric types","Validate parameter types at config-load time, once, not per lookup","Centralize parameter access so type conversions live in one place"],"tags":["type-cast","configuration","parameters","java"],"backgroundTag":"type-mismatch","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}