【问题标题】:Jaxb EclipseLink/MOXy : Is it possible to specify the names of get/set methodsJaxb EclipseLink/MOXy:是否可以指定 get/set 方法的名称
【发布时间】:2012-01-12 12:11:19
【问题描述】:

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

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

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 中指定用于特定属性的 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" /> 

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

提前感谢您的回答。

【问题讨论】:

    标签: jaxb properties eclipselink getter-setter moxy


    【解决方案1】:

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

    【讨论】:

    • xml-accessor-type="FIELD" 是强制性的还是我仍然可以使用 xml-accessor-type="NONE" ?
    • @user1121108 - 我已经更新了我的答案,应该如何在不指定 xml-accessor-type="FIELD" 的情况下做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多