{"record":{"id":"d9f1c083efb1cc79","repo":"dromara/Sa-Token","slug":"10321","errorCode":"10321","errorMessage":"Method 不可以是 null","messagePattern":"Method 不可以是 null","errorType":"exception","errorClass":"SaTokenException","httpStatus":null,"severity":"error","filePath":"sa-token-core/src/main/java/cn/dev33/satoken/router/SaHttpMethod.java","lineNumber":56,"sourceCode":"\t */\n\tALL;\n\t\n\tprivate static final Map<String, SaHttpMethod> map = new ConcurrentHashMap<>();\n\n\tstatic {\n\t\tfor (SaHttpMethod reqMethod : values()) {\n\t\t\tmap.put(reqMethod.name(), reqMethod);\n\t\t}\n\t}\n\n\t/**\n\t * String 转 enum \n\t * @param method 请求类型 \n\t * @return SaHttpMethod 对象\n\t */\n\tpublic static SaHttpMethod toEnum(String method) {\n\t\tif(method == null) {\n\t\t\tthrow new SaTokenException(\"Method 不可以是 null\").setCode(SaErrorCode.CODE_10321);\n\t\t}\n\t\tSaHttpMethod reqMethod = map.get(method.toUpperCase());\n\t\tif(reqMethod == null) {\n\t\t\tthrow new SaTokenException(\"无效Method：\" + method).setCode(SaErrorCode.CODE_10321);\n\t\t}\n\t\treturn reqMethod;\n\t}\n\n\t/**\n\t * String[] 转 enum[]\n\t * @param methods 请求类型数组 \n\t * @return SaHttpMethod 数组\n\t */\n\tpublic static SaHttpMethod[] toEnumArray(String... methods) {\n\t\tSaHttpMethod [] arr = new SaHttpMethod[methods.length];\n\t\tfor (int i = 0; i < methods.length; i++) {\n\t\t\tarr[i] = SaHttpMethod.toEnum(methods[i]);\n\t\t}","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dromara/Sa-Token/blob/ac2c7f6e94a78573cf0bcb932dd8b04e68fad189/sa-token-core/src/main/java/cn/dev33/satoken/router/SaHttpMethod.java#L38-L74","documentation":"SaHttpMethod.toEnum(String) converts an HTTP method string into the SaHttpMethod enum. Before lookup it rejects a null argument with SaTokenException code 10321. This is a plain argument-validation failure: the framework never received a method name to match.","triggerScenarios":"Calling SaHttpMethod.toEnum(null) directly, or SaRouter matching (e.g. SaRouter.match(...).method(...)) / SaHttpMethod.toEnumArray(...) with a request whose method string is null, or a custom filter passing a null method into match API.","commonSituations":"Non-HTTP or mock contexts (unit tests, WebSocket threads, scheduled jobs) where SaHolder.getRequest().getMethod() returns null; a gateway or proxy forwarding a request without a method; programmatic router rules built from nullable config values.","solutions":["Null-check the method string before calling toEnum / toEnumArray and skip or default the match when null.","If the call happens inside a SaRouter rule running outside a real HTTP request, guard with SaRouter.isMatchCurrRequest or run the logic only when SaHolder.getContext() is valid.","Upgrade sa-token if you hit this via framework internals on a normal HTTP request — it may be a bug fixed in a later patch.","Wrap the matching code in try-catch for SaTokenException with code 10321 and treat it as 'no match'."],"exampleFix":"// before\nSaHttpMethod m = SaHttpMethod.toEnum(methodFromConfig); // methodFromConfig may be null\n\n// after\nif (methodFromConfig == null) {\n    return; // or use a default such as SaHttpMethod.GET\n}\nSaHttpMethod m = SaHttpMethod.toEnum(methodFromConfig);","handlingStrategy":"validation","validationCode":"if (method == null) { return / * skip or default * /; }","typeGuard":"static boolean isNonNullMethod(String m) { return m != null; }","tryCatchPattern":"try { SaHttpMethod.toEnum(method); } catch (SaTokenException e) { if (\"10321\".equals(e.getCode())) { /* treat as no-match */ } else throw e; }","preventionTips":["Never build router rules from nullable config values without a null check.","In tests/mocks, pass explicit method strings instead of relying on request context."],"tags":["sa-token","http-method","null-check","router"],"backgroundTag":null,"analyzedSha":"ac2c7f6e94a78573cf0bcb932dd8b04e68fad189","analyzedAt":"2026-08-14T14:36:10.271Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}