Jaxb EclipseLink/MOXy:是否可以指定 get/set 方法的名称

发布于 2024-12-26 13:28:20 字数 1270 浏览 2 评论 0原文

我有一个非常简单的问题:

假设我有一个这样定义的模型类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2025-01-02 13:28:20

您可以在 EclipseLink JAXB (MOXy) 中表示这一点外部映射文档如下:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8834871">
    <java-types>
        <java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
            <xml-root-element/>
            <java-attributes>
                <xml-element 
                    java-attribute="testAttribute" 
                    name="test-attribute">
                    <xml-access-methods 
                        get-method="getFormattedTestAttribute" 
                        set-method="setTestAttribute"/>
                </xml-element>
                <xml-transient java-attribute="formattedTestAttribute"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

测试

我已经修改了您的 Test 类,将一些逻辑放入 get/set 方法中。

package forum8834871;

public class Test{

    private String testAttribute;

    public Test(){
    }

    public String getFormattedTestAttribute(){
       return "APPENDED_ON_GET " + testAttribute;
    }

    public void setTestAttribute(String value){
       testAttribute = "APPENDED_ON_SET " + value;
    }

}

演示

package forum8834871;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8834871/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);

        File xml = new File("src/forum8834871/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Test test = (Test) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(test, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <test-attribute>ORIGINAL</test-attribute>
</test>

输出

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>

You could represent this in EclipseLink JAXB (MOXy)'s external mapping document as follows:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum8834871">
    <java-types>
        <java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
            <xml-root-element/>
            <java-attributes>
                <xml-element 
                    java-attribute="testAttribute" 
                    name="test-attribute">
                    <xml-access-methods 
                        get-method="getFormattedTestAttribute" 
                        set-method="setTestAttribute"/>
                </xml-element>
                <xml-transient java-attribute="formattedTestAttribute"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Test

I have modified your Test class, to put some logic in the get/set methods.

package forum8834871;

public class Test{

    private String testAttribute;

    public Test(){
    }

    public String getFormattedTestAttribute(){
       return "APPENDED_ON_GET " + testAttribute;
    }

    public void setTestAttribute(String value){
       testAttribute = "APPENDED_ON_SET " + value;
    }

}

Demo

package forum8834871;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8834871/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);

        File xml = new File("src/forum8834871/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Test test = (Test) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(test, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <test-attribute>ORIGINAL</test-attribute>
</test>

Output

<?xml version="1.0" encoding="UTF-8"?>
<test>
   <test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文