XStream - 使用属性设置器而不是直接字段访问
我正在尝试使用 XStream 从 XML 加载对象。 默认情况下,XStream 似乎使用直接字段访问。因此,当您将名为“rolesAsString”的字段映射到名为“roles”的别名时,它将从“roles”标记中获取值,并尝试将其直接设置为类中的“rolesAsString”字段。这在大多数情况下有效,但不适用于我的情况。
以下是与问题相关的 XML: <代码>
<principal name="admin" roles="role1, role2, role3">
这是课程: <代码>
public class Principal {
private Set<String> roles = new HashSet<String>();
public Set<String> getRoles() {
return roles;
}
public void setRolesAsString(String roles) {
for (String role : roles.split(",")) {
role = role.trim();
if (!"".equals(role)) { //ignore empty string roles
this.roles.add(role);
}
}
}
public String getRolesAsString() {
StringBuilder result = new StringBuilder();
boolean first = true;
for (String role : roles) {
if (first) {
first = false;
} else {
result.append(", ");
}
result.append(role);
}
return result.toString();
}
我试图在从 xml 文件加载配置时使 XStream 调用 setRolesAsString,而不是尝试直接连接到 Roles 字段。
我尝试过使用 JavaBeanConverter 转换器来做到这一点,但我无法使其工作。我使用的代码是: <代码>
configParser.registerLocalConverter(Principal.class, "roles", new JavaBeanConverter(configParser.getMapper()) {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
return true;//should make XStream to always use this converter for that field.
}
});
configParser.aliasAttribute(Resource.class, "角色", "角色");
知道我在这方面哪里出错了吗?
谢谢, 斯特夫。
I'm trying to load an object from XML using XStream.
By default XStream seems to use direct field access. Thus, when you map a field named 'rolesAsString' to an alias named 'roles', it will take the value from the 'roles' tag and try to set it directly to your 'rolesAsString' field in your class. This works in most cases, but not in my case.
Here is the XML relevant for the question:
<principal name="admin" roles="role1, role2, role3">
Here is the class:
public class Principal { private Set<String> roles = new HashSet<String>(); public Set<String> getRoles() { return roles; }public void setRolesAsString(String roles) { for (String role : roles.split(",")) { role = role.trim(); if (!"".equals(role)) { //ignore empty string roles this.roles.add(role); } } } public String getRolesAsString() { StringBuilder result = new StringBuilder(); boolean first = true; for (String role : roles) { if (first) { first = false; } else { result.append(", "); } result.append(role); } return result.toString(); }
I am trying to make XStream call setRolesAsString when loading the configuration from the xml file, instead of trying to connect directly to the roles field.
I have tried doing that with JavaBeanConverter converter, but I can't make it work. Code I'm using for that is:
configParser.registerLocalConverter(Principal.class, "roles", new JavaBeanConverter(configParser.getMapper()) { @Override public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) { return true;//should make XStream to always use this converter for that field. } });configParser.aliasAttribute(Resource.class, "roles", "roles");
Any idea where I'm going wrong on this?
Thanks,
Stef.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论