6

为了使用 JAXB 编组一个长的原始类型,我使用了@XmlJavaTypeAdapter注解,它将非字符串类型适配为字符串。即使它为长类型抛出错误。为什么会这样?如何对我的longid 属性进行编组?

用户.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();
    }
}

错误:

  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" 
4

4 回答 4

8

在注解中指定原始类:

@XmlJavaTypeAdapter(type=long.class, value=WSLongAdapter.class)
于 2013-11-05T16:32:55.083 回答
5

以下可能有效:

class User {
    @XmlID
    @XmlJavaTypeAdapter(WSLongAdapter.class)
    @XmlElement(type=Long.class)
    private long id;
    // Other variables
    // Getter & Setter method
}    
于 2012-12-27T10:25:51.957 回答
1

@XmlID创建带有返回注释的新 getterString

@XmlAccessorType(XmlAccessType.FIELD)
class User {

    private long id;

    @XmlID
    public String getReferenceId() {
        return Long.toString(id);
    }

}

此解决方案的唯一缺点是序列化 XML 中存在一个附加标记(在这种情况下<referenceId>)。我试图通过注释该 getter 来删除它,@XmlTransient但随后我收到错误消息,通知 ID 在引用的类中不存在。

于 2015-11-17T15:18:31.420 回答
0

只需更换

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

    @XmlSchemaType(name = "long")
protected Long id;
于 2015-02-03T22:35:31.530 回答