3

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

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

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 的标准设置器,但 getter 有一个不同的名称:getFormattedTestProperty()。

是否可以在 Jaxb/Moxy 中指定用于特定属性的吸气剂?

我正在使用带有外部元数据绑定文件的 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" /> 

请注意,我对模型类的控制非常有限。

提前感谢您的回答。

4

1 回答 1

3

您可以在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);
    }

}

输入.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>
于 2012-01-12T14:15:56.357 回答