当前位置: 首页 > 知识库问答 >
问题:

XML反序列化导致JSONMappingException:无法将类实例反序列化出VALUE_STRING标记

林祯
2023-03-14
<game quarter="2">
  <quarter number="1">
    ...
  </quarter>
  <quarter number="2">
    ...
  </quarter>
</game>
@JsonIgnoreProperties(ignoreUnknown = true)
public class Game {

    @JacksonXmlElementWrapper(localName = "quarter")
    public Quarter[] quarters;
xmlMapper.readValue(getFileContent(PBP_XML_PATH), Game.class);
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Game$Quarter[] out of VALUE_STRING token
  at [Source: java.io.StringReader@5f45632; line: 4, column: 1] (through reference chain: Game["quarter"])
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
  at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
  at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:275)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:179)
  at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:20)
  at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499)
  at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:108)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)

看起来杰克逊无法处理同名的属性和属性。

共有1个答案

谭鹏云
2023-03-14

从表面上看,这是Jackson-DataFormat-XML-2.9.0版本中的一个已知问题:

在反序列化过程中不能配置属性/元素名称冲突

如果元素的属性和子元素名称相同,则属性似乎被忽略,而元素值被使用

 类似资料: