I use java spring for my project. I try to access the property and set it with a specific value using the reflection.
I try to access the name property of User class:
@Data
public class User {
@Id
private String id;
private String name;
private String phone;
private String email;
}
Here how I try to access the name field:
User newUser = userRepository.get(id);
User user = accessProp(newUser, User.class, "name", "John");
public <D> D accessProp(Class<D> dest, String fieldName, Object value ){
Field filed = null;
var cls = AdminUser.class;
filed = cls.getField(fieldName);
filed.set(dest, value);
return dest;
}
But on this row:
filed = cls.getField(fieldName);
I get this error:
java.lang.NoSuchFieldException: name
My question is why "name" field not found?
Aucun commentaire:
Enregistrer un commentaire