EclipseLink MOXy: Override rules of binding files

2019-06-03 02:39发布

I would, in the scenario below, like the binding of java-type name="SubClass" to be applied to set the text field on SuperClass. However it is not. Is there a problem with overriding the bindingsA.xml? According to the Overriding rules documentation:

If the same java-type occurs in multiple files, any values that are set in the later file, will override values from the previous file

What do I need to do to make it work?

Input:

<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>

Bindings A:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

 <java-types>        
   <java-type name="SuperClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="@text" />
    </java-attributes>
  </java-type>
 </java-types>

</xml-bindings>

Bindings B:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

<java-types>        
  <java-type name="SuperClass" xml-transient="true"></java-type>
  <java-type name="SubClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="text()" />
    </java-attributes>
  </java-type>
</java-types>

</xml-bindings>

Classes:

public class SuperClass {

 private String text;

 public String getText() {
    return text;
 }

 public void setText(String text) {
    this.text = text;
 }
}

public class SubClass extends SuperClass { }

Demo:

Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
List<String> bindings = new LinkedList<String>();
bindings.add("bindingsA.xml");
bindings.add("bindingsB.xml");
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(superClass.getText()); 

Output:

[EL Warning]: 2013-07-31 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text

1条回答
放我归山
2楼-- · 2019-06-03 03:19

It's a bit odd to map the text property differently on the super and sub classes. If this is something you really want to do, then below is a way you could accomplish this.

Java Model

SuperClass

package forum17982654;

public class SuperClass {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

SubClass

We will override the accessor methods from the super class. This will help us trick MOXy into thinking that SubClass has its own property text.

package forum17982654;

public class SubClass extends SuperClass {

    @Override
    public String getText() {
        return super.getText();
    }

    @Override
    public void setText(String text) {
        super.setText(text);
    } 

}

Metadata

bindings.xml

In the mapping document we will tell MOXy that the real super class of SubClass is java.lang.Object.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17982654">
    <java-types>
        <java-type name="SuperClass">
            <xml-root-element/>
            <java-attributes>
                <xml-attribute java-attribute="text"/>
            </java-attributes>
        </java-type>
        <java-type name="SubClass" super-type="java.lang.Object">
            <xml-root-element/>
            <java-attributes>
                <xml-value java-attribute="text"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Demo Code

Below is some demo code you can run to prove that everything works:

Demo

package forum17982654;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
        jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader superClassXML = new StringReader("<superClass text='Hello Super Class'/>");
        SuperClass superClass = (SuperClass) unmarshaller.unmarshal(superClassXML);
        System.out.println(superClass.getText());

        StringReader subClassXML = new StringReader("<subClass>Hello Sub Class</subClass>");
        SubClass subClass = (SubClass) unmarshaller.unmarshal(subClassXML);
        System.out.println(subClass.getText());
    }

}

Output

Hello Super Class
Hello Sub Class
查看更多
登录 后发表回答