Jaxb EclipseLink/MOXy:是否可以指定 get/set 方法的名称
我有一个非常简单的问题:
假设我有一个这样定义的模型类:
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return testAttribute + "A nice formatted thingy"; //right, this is just an example
}
public void setTestAttribute(String value){
testAttribute = value;
}
}
您可以看到我有一个 testProperty 的标准 setter,但 getter 有一个不同的名称:getFormattedTestProperty()。
Jaxb/Moxy 是否可以指定针对特定属性使用哪个 getter?
我正在使用带有外部元数据绑定文件的 MOXy 实现。我正在做的项目使用了 Castor。在 Castor 的映射文件中,您可以指定要使用的 getter/setter,如下所示:
<field name="testAttribute"
get-method="getFormattedTestAttribute">
<bind-xml name="test-attribute" node="attribute"/>
</field>
对于 moxy 的外部元数据是否可以做同样的事情?
如果不支持这种自定义,是否可以将一个字段标记为只读,将另一个字段标记为只写?这样我就可以在元数据绑定文件中声明一个名为“formattedTestAttribute”的只读属性和一个名为“testAttribute”的只写属性?
<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="@test-attribute" />
<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="@test-attribute" />
请注意,我对模型类的控制非常有限。
预先感谢您的回答。
I have a quite simple question :
Say I have a model class defined like this :
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return testAttribute + "A nice formatted thingy"; //right, this is just an example
}
public void setTestAttribute(String value){
testAttribute = value;
}
}
You can see that I have a standard setter for testProperty but the getter has a different name : getFormattedTestProperty().
Is it possible into Jaxb/Moxy to specify which getter to use for a specific property ?
I'm using MOXy implementation with external metadata bindings file. The project which I'm working on used tu use Castor. Into Castor's mapping files, you could specify which getter/setter to use like that :
<field name="testAttribute"
get-method="getFormattedTestAttribute">
<bind-xml name="test-attribute" node="attribute"/>
</field>
Is the same kind of thing possible with moxy's external metadata ?
If that kind of customization isn't supported, is it possible to mark a field as read-only and another as write-only ? so I could declare a read-only property named "formattedTestAttribute" and a write-only property named "testAttribute" into the metadata bindings file ?
<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="@test-attribute" />
<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="@test-attribute" />
Please note that I have very limited control over the model classes.
Thanks in advance for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 EclipseLink JAXB (MOXy) 中表示这一点外部映射文档如下:
测试
我已经修改了您的
Test
类,将一些逻辑放入 get/set 方法中。演示
input.xml
输出
You could represent this in EclipseLink JAXB (MOXy)'s external mapping document as follows:
Test
I have modified your
Test
class, to put some logic in the get/set methods.Demo
input.xml
Output