dromara/Sa-Token · error · RuntimeException
属性取值出错:${fieldName}
Error message
属性取值出错:${fieldName} What it means
Thrown inside SoMap's reflection copy loop (getModelValue-style) when Field.set fails with IllegalArgumentException or IllegalAccessException while copying a map entry onto a bean field. The message names the offending fieldName; the original reflection exception is chained as the cause.
Source
Thrown at sa-token-demo/sa-token-demo-oauth2/sa-token-demo-oauth2-client/src/main/java/com/pj/utils/SoMap.java:234
}
/** 从map中取值,塞到一个对象中 */
public <T> T getModelByObject(T obj) {
// 获取类型
Class<?> cs = obj.getClass();
// 循环复制
for (Field field : cs.getDeclaredFields()) {
try {
// 获取对象
Object value = this.get(field.getName());
if(value == null) {
continue;
}
field.setAccessible(true);
Object valueConvert = getValueByClass(value, field.getType());
field.set(obj, valueConvert);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("属性取值出错:" + field.getName(), e);
}
}
return obj;
}
/**
* 将指定值转化为指定类型并返回
* @param obj
* @param cs
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getValueByClass(Object obj, Class<T> cs) {
String obj2 = String.valueOf(obj);
Object obj3 = null;View on GitHub (pinned to ac2c7f6e94)
Solutions
- Read the chained cause (e.getCause()) to see whether it is an argument-type or access failure.
- Align parameter types: ensure the map value for the named field is convertible (e.g. numeric string into int is fine, but a Map into int is not).
- If IllegalAccessException under JDK 16+, add --add-opens opens for the affected package or avoid reflecting into JDK internals.
- Rename mismatched keys or add @JsonIgnore-equivalent handling so unconvertible entries are skipped instead of fatal.
Example fix
// before
User user = soMap.getModel("user", User.class);
// after
try {
User user = soMap.getModel("user", User.class);
} catch (RuntimeException e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
log.warn("bind failed: {}", cause.getMessage());
throw new IllegalArgumentException("invalid user payload");
} Defensive patterns
Strategy: try-catch
Try / catch
try { User u = soMap.getModel("user", User.class); } catch (RuntimeException e) { Throwable c = e.getCause() != null ? e.getCause() : e; if (c instanceof IllegalAccessException) { /* add-opens / access issue */ } else { /* type mismatch on named field */ } } Prevention
- Keep bean field types aligned with the parameter types the client sends (avoid primitives for optional fields).
- On JDK 16+, add required --add-opens JVM args for any packages you reflect into.
- Always inspect the chained cause — the field name in the message plus cause type pinpoints the bad entry.
When it happens
Trigger: Calling soMap.getModel(key, BeanClass) / the field-copy loop when a map value cannot be converted to the bean field's declared type by getValueByClass (IllegalArgumentException), or the field belongs to a module/class where setAccessible is denied (IllegalAccessException, e.g. JDK 17+ strong encapsulation of java.* internals).
Common situations: A request parameter that is a String (e.g. "123") being pushed into a field whose converter path fails, a null primitive field, or running the demo on JDK 16+ where reflective access to certain classes is blocked by default.
Related errors
AI-assisted analysis of dromara/Sa-Token@ac2c7f6e94 (2026-08-14).
Data as JSON: /api/errors/c41d66063f571e37.
Report an issue: GitHub.