【问题标题】:Marshaling a long primitive type using JAXB使用 JAXB 编组长原始类型
【发布时间】:2012-12-27 10:23:13
【问题描述】:

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

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

错误:

  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" 

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    在注解中指定原始类:

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

    【讨论】:

    • 这个解决方案帮助我将一个布尔值映射到一个空对象,目的是将 true 呈现为 并将 false 呈现为空。
    【解决方案2】:

    以下方法可能有效:

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

    【讨论】:

    • 谢谢,但还是同样的错误。 :( . 难道不能编组长类型吗?
    • @Bhumika - 可以使用 JAXB (JSR-222) 实现来编组 long 类型。您看到的问题是参考 impl 不喜欢非字符串类型上的 @XmlID 注释(这对 EclipseLink JAXB (MOXy) 来说是可以的)。您只需将@XmlID@XmlIDREF 结合使用(请参阅:blog.bdoughan.com/2010/10/…)这是您想要做的吗?
    • 好吧,其实我想实现一对多的关系。即用户有一个组织,我面临的问题是“在对象图中检测到一个循环。这将导致无限深的 XML”。所以我找到了一个解决方案,如“通过@XmlIDREF@XMLID 的帮助,JAXB 将在生成 xml 图时使用object id 而不是object”。但我被困在很长的原始岗位上。所以我正在尝试使用适配器,但仍然是同样的问题。
    • @Bhumika - 你能把字段改成Long而不是long吗?您仍然可以将 get/set 方法保留为 long
    • @Bhumika - 您可以使用 EclipseLink MOXy 作为您的 JAXB (JSR-222) 提供程序,然后您可以在 long 字段上添加 @XmlID 注释(注意:我是 MOXy 负责人) :blog.bdoughan.com/2011/05/…
    【解决方案3】:

    创建带有@XmlID 注释的新getter,返回String

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

    此解决方案的唯一缺点是序列化 XML 中存在一个附加标记(在本例中为 &lt;referenceId&gt;)。我试图通过使用 @XmlTransient 注释该 getter 来删除它,但随后我收到错误消息,提示 ID 不存在于引用的类中。

    【讨论】:

      【解决方案4】:

      只需替换

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多