Marshaling a long primitive type using JAXB

前端 未结 4 1893
小鲜肉
小鲜肉 2021-01-18 10:08

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

相关标签:
4条回答
  • 2021-01-18 10:53

    Just replace

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

    with

        @XmlSchemaType(name = "long")
    protected Long id;
    
    0 讨论(0)
  • 2021-01-18 11:02

    Specify the primitive class in the annotation:

    @XmlJavaTypeAdapter(type=long.class, value=WSLongAdapter.class)
    
    0 讨论(0)
  • 2021-01-18 11:11

    The following might work:

    class User {
        @XmlID
        @XmlJavaTypeAdapter(WSLongAdapter.class)
        @XmlElement(type=Long.class)
        private long id;
        // Other variables
        // Getter & Setter method
    }    
    
    0 讨论(0)
  • 2021-01-18 11:14

    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.

    0 讨论(0)
提交回复
热议问题