JAXB 2.x: abstract methods combine as an attribute

I have an abstract root class, say A

And I have several implementation classes extending A

A has FIELD annotation, as well as some annotated @XmlElement properties.

A also has an abstract method.

When sorting ( B extends A ), the value returned by the abstract method gets the attribute. Not as expected, right?

 @XmlAccessorType(XmlAccessType.FIELD) public abstract class SpecialProfile extends ContentNodeBean { @XmlElement(name="do-index", namespace="my") private boolean doIndex = false; public abstract SpecialProfileType getSpecialProfileType(); ... getters and setters for properties ... } 

Does anyone have one problem and how can this be fixed?

I am using org.eclipse.persistence.moxy 2.1.2

+4
source share
1 answer

I am trying to reproduce your problem, but still have not been successful. Do you see where I am doing something other than you? The following is sample code:

A

 import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public abstract class A { public abstract C getC(); public abstract void setC(C c); } 

IN

 import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class B extends A { private C c; @Override public C getC() { return c; } @Override public void setC(C c) { this.c = c; } } 

WITH

 public class C { } 

Demo

 import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import org.eclipse.persistence.Version; public class Demo { public static void main(String[] args) throws Exception { System.out.println(Version.getVersionString()); JAXBContext jc = JAXBContext.newInstance(B.class); System.out.println(jc); B b = new B(); b.setC(new C()); Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(b,System.out); } } 

Output

 2.1.2.v20101206-r8635 org.eclipse.persistence.jaxb.JAXBContext@100ab23 <?xml version="1.0" encoding="UTF-8"?> <b xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"><c/></b> 

UPDATE

Based on your comments:

  • B does not inherit XmlAccessorType settings.
  • This is not an abstract method that you need to point out @XmlTransient, but the field used to implement the accessor in class B.

What follows is what class B looks like:

 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class B extends A { @XmlTransient private C c; @Override public C getC() { return c; } @Override public void setC(C c) { this.c = c; } } 
+1
source

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


All Articles