Java 杰克逊:“意外的令牌(VALUE_STRING),预期的 FIELD_NAME:”反序列化空字符串而不是预期的对象时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19765240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson: "Unexpected token (VALUE_STRING), expected FIELD_NAME:" when deserializing empty string instead of expected object
提问by CtrlF
I'm using Hymanson to deserialize json responses from a server I do not own. I'm using JsonTypeInfo annotations to handle polymorphic data types. Here's the configuration I have on my base type (Thing
in this case):
我正在使用 Hymanson 反序列化来自我不拥有的服务器的 json 响应。我正在使用 JsonTypeInfo 注释来处理多态数据类型。这是我对基本类型的配置(Thing
在本例中):
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Thing.class, name = "Thing"),
@JsonSubTypes.Type(value = FancyThing.class, name = "FancyThing")
})
Everything works great until the server returns an empty string where I am expecting one of these types and then I get one of these:
一切都很好,直到服务器返回一个空字符串,我期待其中一种类型,然后我得到其中一种:
org.codehaus.Hymanson.map.JsonMappingException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.stackoverflow.Thing)
org.codehaus.Hymanson.map.JsonMappingException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.stackoverflow.Thing)
Is there a recommended way of handling cases like this? Like I said, I don't control the server, so I have to handle this client side. I'd rather handle this by configuring the ObjectMapper
, but ObjectMapper#configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
doesn't seem to work the way I had expected. Any ideas?
有没有推荐的处理这种情况的方法?就像我说的,我不控制服务器,所以我必须处理这个客户端。我宁愿通过配置 来处理这个问题ObjectMapper
,但ObjectMapper#configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
似乎并没有像我预期的那样工作。有任何想法吗?
采纳答案by CtrlF
Ok, so sometimes all it takes to get an answer is to ask yourself the right question. It turns out, I think I was looking at an older version of the javadocs, because I discovered @JsonTypeInfo#defaultImpl
and that has set me on to the right track.
好的,所以有时获得答案所需的只是问自己正确的问题。事实证明,我认为我正在查看 javadoc 的旧版本,因为我发现@JsonTypeInfo#defaultImpl
并且这使我走上了正确的轨道。
Now I have something that looks like this:
现在我有一些看起来像这样的东西:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
defaultImpl = EmptyThing.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = Thing.class, name = "Thing"),
@JsonSubTypes.Type(value = FancyThing.class, name = "FancyThing")
})
public class Thing {
...
}
public class EmptyThing extends Thing {
public EmptyThing(String s) {
// Hymanson wants a single-string constructor for the default impl
}
}