dromara/Sa-Token · error · SaTokenException
10321
10321
Error message
Method 不可以是 null
What it means
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.
Source
Thrown at sa-token-core/src/main/java/cn/dev33/satoken/router/SaHttpMethod.java:56
*/
ALL;
private static final Map<String, SaHttpMethod> map = new ConcurrentHashMap<>();
static {
for (SaHttpMethod reqMethod : values()) {
map.put(reqMethod.name(), reqMethod);
}
}
/**
* String 转 enum
* @param method 请求类型
* @return SaHttpMethod 对象
*/
public static SaHttpMethod toEnum(String method) {
if(method == null) {
throw new SaTokenException("Method 不可以是 null").setCode(SaErrorCode.CODE_10321);
}
SaHttpMethod reqMethod = map.get(method.toUpperCase());
if(reqMethod == null) {
throw new SaTokenException("无效Method:" + method).setCode(SaErrorCode.CODE_10321);
}
return reqMethod;
}
/**
* String[] 转 enum[]
* @param methods 请求类型数组
* @return SaHttpMethod 数组
*/
public static SaHttpMethod[] toEnumArray(String... methods) {
SaHttpMethod [] arr = new SaHttpMethod[methods.length];
for (int i = 0; i < methods.length; i++) {
arr[i] = SaHttpMethod.toEnum(methods[i]);
}View on GitHub (pinned to ac2c7f6e94)
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'.
Example fix
// before
SaHttpMethod m = SaHttpMethod.toEnum(methodFromConfig); // methodFromConfig may be null
// after
if (methodFromConfig == null) {
return; // or use a default such as SaHttpMethod.GET
}
SaHttpMethod m = SaHttpMethod.toEnum(methodFromConfig); Defensive patterns
Strategy: validation
Validate before calling
if (method == null) { return / * skip or default * /; } Type guard
static boolean isNonNullMethod(String m) { return m != null; } Try / catch
try { SaHttpMethod.toEnum(method); } catch (SaTokenException e) { if ("10321".equals(e.getCode())) { /* treat as no-match */ } else throw e; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of dromara/Sa-Token@ac2c7f6e94 (2026-08-14).
Data as JSON: /api/errors/d9f1c083efb1cc79.
Report an issue: GitHub.