Jackson:不能从START_OBJECT标记反序列化Number的实例 [英] Jackson: Can not deserialize instance of Number out of START_OBJECT token
问题描述
我的GWT服务返回 LinkedList< VisualData>
。这就是 VisualData
的样子:
My GWT service returns LinkedList<VisualData>
. This is how VisualData
looks:
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;
}
}
我将以下excepion与字段 private数值
。
I get the following excepion connected with field 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"])
当我将私人数值
更改为私人对象值
时,我得到的所有获得者和设置者:
When I change private Number value
to private Object value
, all getters and setters I get:
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
class不可序列化。但为什么我得到不能从START_OBJECT标记中反序列化java.lang.Number的实例
?
This second case is quite clear, the Object
class is not serializable. But why do I get Can not deserialize instance of java.lang.Number out of START_OBJECT token
?
推荐答案
如果没有为值
字段提供额外的类型信息,您将无法将数据反序列化到此对象中。这是因为 Number
类是抽象的,不能被实例化。将字段更改为 Object
将无济于事,因为该类没有可写的字段,Jackson可以反序列化数据。
You won't be able to deserialize data into this object without supplying additional type information for your value
field. This is because the Number
class is abstract and cannot be instantiated. Changing the field to Object
will not help, since there are no writable fields on that class that Jackson can deserialize data into.
您应该将字段更改为 Number
类(整数
, Long
, Double
等等)。
You should change the field to be one of the concrete implementations of the Number
class (Integer
, Long
, Double
etc).
这篇关于Jackson:不能从START_OBJECT标记反序列化Number的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!