【发布时间】:2017-03-31 17:21:42
【问题描述】:
我从来没有遇到过这样的要求,我很困惑如何实现它。
让我们考虑以下 Bean:
Company{
String companyName;
String companyId;
Person person;
//getter and setters
}
Person{
String id;
String name;
String lastName;
List<Address> address;
//getter and setters
}
Address{
String id;
String name;
String description;
//getter and setters
}
所以层次结构是这样的:
公司有人有地址
我要做的是,用其他字符串替换所有字符串字段,例如
Company.companyName = "Hi there #xyz"
Address.name = "St. Example #xyz"
例如,我需要用#abcd 替换字符#xyx。
对象比这复杂得多,并且具有巨大的层次结构。
我试图找到一些可以帮助我做到这一点的 API,但是我找不到任何人(我不知道看起来是否正确)。
我的解决方案是在每个 getter 方法中替换字符,但我认为这不是解决该问题的最佳方法。
感谢您的帮助。
提前致谢。
【问题讨论】:
-
你有没有考虑将每个相关的String字段分配给应用String.replace()的结果?
-
@AndyThomas 我将它作为 XML 接收,然后使用 Jaxb 将其转换为对象
标签: java string object reflection