JAXB inheritance in MOXY

I have two classes:

package a; class A { private <type> fieldOfClassA; // getters, and setters } package b; class B extends A{ private <type> fieldOfClassB; // getters, and setters } 

I want to arrange class B on an xml element and add the attribute field OffClassB and fieldOfClassA from class A, but during the sorting process it displays the following warning message:

 Ignoring attribute [fieldOfClassA] on class [bB] as no Property was generated for it. 

Note that the two classes belong to two different packages, and I cannot change this object model.

Thank you in advance!

EDIT:

I use external bind files.

+4
source share
1 answer

From the log message you sent, I see that you are using an external MOXy mapping file (see http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html ). There are several ways to match an inherited property.


OPTION No. 1 - Display the inherited property belonging to the parent

By default, the field / property must be mapped in the class to which it belongs. Since MOXy displays an external mapping document at the package level, you will need separate documents for mapping for A and B

forum10874711 / a / binding1.xml

 <?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.a"> <java-types> <java-type name="A"> <java-attributes> <xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/> </java-attributes> </java-type> </java-types> </xml-bindings> 

forum10874711 / b / binding1.xml

 <?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.b"> <java-types> <java-type name="B"> <xml-root-element/> <java-attributes> <xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/> </java-attributes> </java-type> </java-types> </xml-bindings> 

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.bB; public class Demo1 { public static void main(String[] args) throws Exception { Map<String, Object> properties = new HashMap<String, Object>(1); List<String> metadata = new ArrayList<String>(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); } } 

Exit

 <?xml version="1.0" encoding="UTF-8"?> <b> <field-of-class-a>foo</field-of-class-a> <field-of-class-b>123</field-of-class-b> </b> 

OPTION # 2 - displays a child’s inherited property

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

forum10874711 / a / binding2.xml

 <?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.a"> <java-types> <java-type name="A" xml-transient="true"/> </java-types> </xml-bindings> 

forum10874711 / b / binding2.xml

 <?xml version="1.0" encoding="UTF-8"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10874711.b"> <java-types> <java-type name="B"> <xml-root-element/> <java-attributes> <xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/> <xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/> </java-attributes> </java-type> </java-types> </xml-bindings> 

Demo

 package forum10874711; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextFactory; import forum10874711.bB; public class Demo2 { public static void main(String[] args) throws Exception { Map<String, Object> properties = new HashMap<String, Object>(1); List<String> metadata = new ArrayList<String>(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); } } 

Exit

 <?xml version="1.0" encoding="UTF-8"?> <b> <field-of-class-a>foo</field-of-class-a> <field-of-class-b>123</field-of-class-b> </b> 
+2
source

Source: https://habr.com/ru/post/1415874/


All Articles