简体   繁体   English

泽西岛无法解组java.lang.Number

[英]Jersey cannot unmarshall java.lang.Number

I have a generic (POJO-)container to share statistical data with clients: 我有一个通用(POJO-)容器与客户共享统计数据:

@XmlRootElement
public class StatsSeries implements Serializable {

    private TreeMap<Date, Number> timeSeries;

    /* accessor methods */
}

Depending on the data, the server stores Long , Integer or Double in it, which is why I'm using abstract java.lang.Number . 根据数据,服务器在其中存储LongIntegerDouble ,这就是为什么我使用抽象java.lang.Number

Marshalling works fine, and hints indicating the concrete class are included in the data: 编组工作正常,数据中包含指示具体类的提示:

            "timeSeries": {
                "entry": [
                    {
                        "key": "2012-08-20T00:00:00Z", 
                        "value": {
                            "$": "24", 
                            "@type": "xs:long"
                        }
                    }, 
                    ....
                 ]
              }

Or in XML representation: 或以XML表示:

    <timeSeries>
      <entry>
        <key>2012-08-20T00:00:00Z</key>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:long">24</value>
      </entry>
      ...
    </timeSeries>

When attempting to unmarshall this, I get an javax.xml.bind.UnmarshalException: Unable to create an instance of java.lang.Number . 尝试取消编组时,出现javax.xml.bind.UnmarshalException: Unable to create an instance of java.lang.Number

I saw this question , but it doesn't help me. 我看到了这个问题 ,但并没有帮助我。 How can I annotate java.lang.Number ? 如何注释java.lang.Number Any other suggestions? 还有其他建议吗?

Update : Looking at JAXB-890 , I understand that it should be fixed either on JDK 1.7 or using com.sun.xml.bind:jaxb-impl:2.2.6 -- neither works for me. 更新 :查看JAXB-890 ,我知道应该在JDK 1.7上或使用com.sun.xml.bind:jaxb-impl:2.2.6对其进行修复-都不适合我。

Not sure this will work given type-erasure, but you might try annotating with XmlElementRef 不确定在给定类型擦除的情况下是否可以使用,但是您可以尝试使用XmlElementRef注释

    @XmlElementRef
    private TreeMap<Date, Number> timeSeries;

"when this annotation is used, the XML element name is derived from the instance of the type of the JavaBean property at runtime". “使用此注释时,XML元素名称是在运行时从JavaBean属性类型的实例派生的”。

Failing that, see if this link helps . 如果失败,请查看此链接是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM