【问题标题】:Jackson: Can not deserialize instance of Number out of START_OBJECT token杰克逊:无法反序列化 START_OBJECT 令牌中的 Number 实例
【发布时间】:2013-02-07 13:03:10
【问题描述】:

我的 GWT 服务返回 LinkedList<VisualData>。这就是VisualData 的样子:

import javax.xml.bind.annotation.XmlRootElement;
import com.google.gwt.user.client.rpc.IsSerializable;

@XmlRootElement
public class VisualData implements IsSerializable {
    private Number value;
    private long timestamp;

    public VisualData() {
    }

    public VisualData(Number value, long timestamp) {
        this.value = value;
        this.timestamp = timestamp;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public Number getValue() {
        return value;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public void setValue(Number value) {
        this.value = value;
    }
}

我得到以下与字段 private Number value 相关的异常。

SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.Number out of START_OBJECT token
at [Source: org.apache.catalina.connector.CoyoteInputStream@a0eb51; line: 1, column: 29] (through reference chain: org.jage.charts.client.VisualData["value"])

当我将 private Number value 更改为 private Object value 时,我得到了所有的 getter 和 setter:

SEVERE: WebModule[/AgECharts]Exception while dispatching incoming RPC call
com.google.gwt.user.client.rpc.SerializationException: Type 'org.jage.charts.client.VisualData' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = Value:{@type=xs:int, $=6}, timestamp:1360240281439
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)

第二种情况很清楚,Object 类不可序列化。但是为什么我会得到Can not deserialize instance of java.lang.Number out of START_OBJECT token

【问题讨论】:

标签: java json gwt serialization jackson


【解决方案1】:

如果不为 value 字段提供额外的类型信息,您将无法将数据反序列化到此对象中。这是因为Number 类是抽象的,无法实例化。将字段更改为 Object 将无济于事,因为该类中没有杰克逊可以将数据反序列化到的可写字段。

您应该将该字段更改为Number 类(IntegerLongDouble 等)的具体实现之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2021-09-05
    • 2015-03-02
    • 2020-07-22
    • 2019-06-01
    • 2019-12-22
    • 2019-02-12
    相关资源
    最近更新 更多