I have a bindings file with the following content:
<java-type name="JavaType">
<xml-root-element name="root"/>
<java-attributes>
...
</java-attributes>
</java-type>
When I marshall the JavaType class using this binding, the XML looks like this
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="JavaType">
I don't want the xsi:type to be there, how can I suppress this when marshalling?
The xsi:type
attribute will appear when you are marshalling a subclass. You can have it be suppressed by wrapping your object in a JAXBElement
that supplies information about the root element including type.
JAXBElement<JavaType> je = new JAXBElement(new QName(), JavaType.class javaType);
marshaller.marshal(je, System.out);
Example
UPDATE
Thanks. I now made the superclass XmlTransient, which makes the xsi:type disapear as well. I used the annotation to do that. Is there actually a way to use to make a java-type be transient? I could only make it work for java-attributes.
You are correct. You can use @XmlTransient
at the class level to have it removed from the inheritance hierarchy. Below is how this can be done using MOXy's external mapping document.
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
package-name="com.example.foo">
<java-types>
<java-type name="Foo" xml-transient="true"></java-type>
</java-types>
</xml-bindings>
For More Information
I tried @blaise-doughan's suggestion and added the @XmlTransient
annotation to top of my abstract base class.
With my environment (so my product depends something strictly), the jaxb-api library says " The annotation @XmlTransient
is disallowed for this location " because of the version of it high probably older than 2.1 . I realized that when I tried to adding the annotation with a new test class-path which contains version >=2.1 , so it allows to defining it top of a class.
So let me get straight to the point, I suggest below method in order to get rid of the type fields which appears on responses which are building and marshaling from extended classes.
I only added @XmlDiscriminatorNode("")
to top of my base class and I supposed that you are using the EclipseLink MOXy:
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode; @XmlDiscriminatorNode("") public abstract class Base { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } }
As a summary you can use @XmlTransient
if you have the jaxb-api version greater than 2.1 or use my method if you have EclipseLink MOXy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.