Marshaling a long primitive type using JAXB

To marshal a long primitive type using JAXB, I used the @ XmlJavaTypeAdapter annotation, which will adapt the non-string type to String. Despite the fact that it causes an error for a long type. Why is this so? How can I sort by id long attribute?

User.java

 class User { @XmlID @XmlJavaTypeAdapter(WSLongAdapter.class) private long id; // Other variables // Getter & Setter method } 

WSLongAdapter.java

  public class WSLongAdapter extends XmlAdapter<String, Long>{ @Override public String marshal(Long id) throws Exception { if(id==null) return "" ; return id.toString(); } @Override public Long unmarshal(String id) throws Exception { return Long.parseLong(id); } } 

MarshallTest.java

 public static void main(String[] args) { try{ JAXBContext jaxbContext= JAXBContext.newInstance(User.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); OutputStreamWriter writer = new OutputStreamWriter(System.out); // Manually open the root element writer.write("<user>"); // Marshal the objects out individually marshaller.marshal(new User(), writer); // Manually close the root element writer.write("</user>"); writer.close(); } catch (Exception e) { e.printStackTrace(); } } 

Error:

  com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions Adapter com.v4common.shared.util.other.WSLongAdapter is not applicable to the field type long. this problem is related to the following location: at @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(type=class javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter$DEFAULT, value=class com.v4common.shared.util.other.WSLongAdapter) at private long com.v4common.shared.beans.usermanagement.User.id at com.v4common.shared.beans.usermanagement.User Property "id" has an XmlID annotation but its type is not String. this problem is related to the following location: at private long com.v4common.shared.beans.usermanagement.User.id at com.v4common.shared.beans.usermanagement.User There are two properties named "id" 
+5
source share
4 answers

The following may work:

 class User { @XmlID @XmlJavaTypeAdapter(WSLongAdapter.class) @XmlElement(type=Long.class) private long id; // Other variables // Getter & Setter method } 
+4
source

Specify a primitive class in the annotation:

 @XmlJavaTypeAdapter(type=long.class, value=WSLongAdapter.class) 
+6
source

Create a new getter annotated with @XmlID that returns String

 @XmlAccessorType(XmlAccessType.FIELD) class User { private long id; @XmlID public String getReferenceId() { return Long.toString(id); } } 

The only drawback of this solution is the additional tag present in serialized XML (in this case <referenceId> ). I tried removing it by annotating this getter with @XmlTransient , but then I received an error message indicating that the identifier does not exist in the reference class.

+1
source

Just replace

  @XmlElement(type=Long.class) private long id; 

with

  @XmlSchemaType(name = "long") protected Long id; 
0
source

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


All Articles