To marshal a long primitive type using JAXB, I have used @XmlJavaTypeAdapter
annotation, which will adapt non-String type to a String. Even though it throw erro
Just replace
@XmlElement(type=Long.class)
private long id;
with
@XmlSchemaType(name = "long")
protected Long id;
Specify the primitive class in the annotation:
@XmlJavaTypeAdapter(type=long.class, value=WSLongAdapter.class)
The following might work:
class User {
@XmlID
@XmlJavaTypeAdapter(WSLongAdapter.class)
@XmlElement(type=Long.class)
private long id;
// Other variables
// Getter & Setter method
}
Create new getter annotated with @XmlID
that returns String
@XmlAccessorType(XmlAccessType.FIELD)
class User {
private long id;
@XmlID
public String getReferenceId() {
return Long.toString(id);
}
}
Only disadvantage of this solution is an additional tag is present in serialized XML (in this case <referenceId>
). I tried to remove it by annotating that getter with @XmlTransient
but then I got error informing that ID does not exists in referenced class.