JAXB inheritance in MOXY

后端 未结 1 1565
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 01:07

I have two classes:

package a;
class A {
 private  fieldOfClassA;
 // getters, and setters
}

package b;
class B extends A{
 private          


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 01:20

    From the log message you posted, I can see that you are using MOXy's external mapping document (see http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html). There are a couple of different ways to map the inherited property.


    OPTION #1 - Map the Inherited Property Belonging to the Parent

    By default a field/property needs to be mapped within the class it belongs to. Since MOXy scopes the external mapping document at the package level, you will need separate mapping documents for A and B.

    forum10874711/a/binding1.xml

    
    
        
            
                
                    
                
            
        
    
    

    forum10874711/b/binding1.xml

    
    
        
            
                
                
                    
                
            
        
    
    

    forum10874711/b/jaxb.properties

    To specify MOXy as your JAXB implementation you need to add a file called jaxb.properties in the same package as your domain model with the following entry.

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    package forum10874711;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    import forum10874711.b.B;
    
    public class Demo1 {
    
        public static void main(String[] args) throws Exception {
            Map properties = new HashMap(1);
            List metadata = new ArrayList(2);
            metadata.add("forum10874711/a/binding1.xml");
            metadata.add("forum10874711/b/binding1.xml");
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties);
    
            B b = new B();
            b.setFieldOfClassA("foo");
            b.setFieldOfClassB(123);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(b, System.out);
        }
    
    }
    

    Output

    
    
       foo
       123
    
    

    OPTION #2 - Map the Inherited Property Belonging to Child

    The parent class A' can be marked@XmlTransientthis will allow us to map the inherited fields/properties on the child classB` (see http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html).

    forum10874711/a/binding2.xml

    
    
        
            
        
    
    

    forum10874711/b/binding2.xml

    
    
        
            
                
                
                    
                    
                
            
        
    
    

    Demo

    package forum10874711;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    import forum10874711.b.B;
    
    public class Demo2 {
    
        public static void main(String[] args) throws Exception {
            Map properties = new HashMap(1);
            List metadata = new ArrayList(2);
            metadata.add("forum10874711/a/binding2.xml");
            metadata.add("forum10874711/b/binding2.xml");
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties);
    
            B b = new B();
            b.setFieldOfClassA("foo");
            b.setFieldOfClassB(123);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(b, System.out);
        }
    
    }
    

    Output

    
    
       foo
       123
    
    

    0 讨论(0)
提交回复
热议问题