Tencent/APIJSON · error · UnsupportedDataTypeException

visitorId 只能是 Long 或 String 类型!

Error message

visitorId 只能是 Long 或 String 类型!

What it means

The final branch of verifyLogin(): visitorId is neither Number nor String (e.g. Boolean, Map, JSONObject), so UnsupportedDataTypeException declares that visitorId must be Long or String. This is an integration bug — the object attached as the visitor identity has the wrong runtime type, and the verifier refuses to guess.

Source

Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java:487

	@Override
	public void verifyLogin() throws Exception {
		//未登录没有权限操作
		if (visitorId == null) {
			throw new NotLoggedInException("未登录或登录过期,请登录后再操作!");
		}

		if (visitorId instanceof Number) {
			if (((Number) visitorId).longValue() <= 0) {
				throw new NotLoggedInException("未登录或登录过期,请登录后再操作!");
			}
		}
		else if (visitorId instanceof String) {
			if (StringUtil.isEmpty(visitorId, true)) {
				throw new NotLoggedInException("未登录或登录过期,请登录后再操作!");
			}
		}
		else {
			throw new UnsupportedDataTypeException("visitorId 只能是 Long 或 String 类型!");
		}

	}

	@Override
	public void verifyAdmin() throws Exception {
		throw new UnsupportedOperationException("不支持 ADMIN 角色!如果要支持就在子类重写这个方法" +
                "来校验 ADMIN 角色,不通过则 throw IllegalAccessException!");
	}


	/**验证是否重复
	 * FIXME 这个方法实际上没有被使用
	 * @param table
	 * @param key
	 * @param value
	 * @throws Exception
	 */

View on GitHub (pinned to 5284052872)

Solutions

  1. Assign only the id scalar: visitorId = user.getLong(idKey) (Long) or the username string.
  2. Keep the full user object in a separate field of your verifier/session, not in visitorId.
  3. Add a type check at the point where the visitor is attached: assert visitorId instanceof Number || instanceof String.

Example fix

// before
verifier.visitorId = session.getAttribute("user"); // JSONObject

// after
JSONObject user = (JSONObject) session.getAttribute("user");
verifier.visitorId = user.getLong("id");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(visitorId instanceof Number) && !(visitorId instanceof String)) {
    throw new IllegalStateException("visitorId must be Long or String, got " + visitorId.getClass());
}

Type guard

boolean isSupportedVisitorId(Object v) { return v == null || v instanceof Number || v instanceof String; }

Try / catch

catch (UnsupportedDataTypeException e) { 500 integration bug (not client error); page the owner; fix the visitor attachment code.

Prevention

When it happens

Trigger: A custom Parser/Verifier sets visitorId to a JSONObject of user fields, a Map, or a boolean flag instead of the id scalar; the first verifyLogin() call on a role-protected request hits the else branch.

Common situations: Subclass stores the whole user object as visitorId for convenience; generic deserialization (e.g. from JWT claims) yields a Map; migration changed visitorId type and one code path still assigns the old type.

Related errors


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/8a62bc3880c5222a. Report an issue: GitHub.