XML反序列化导致JsonMappingException:无法从VALUE_STRING标记

时间:2017-03-04 16:43:59

标签: java xml jackson

我有这样的xml:

<game quarter="2">
  <quarter number="1">
    ...
  </quarter>
  <quarter number="2">
    ...
  </quarter>
</game>

我的POJO课程:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Game {

    @JacksonXmlElementWrapper(localName = "quarter")
    public Quarter[] quarters;

当我尝试使用:

反序列化xml时
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 个答案:

答案 0 :(得分:1)

看起来,它是jackson-dataformat-xml-2.9.0版本中的一个已知问题:

Attribute / element name collision cannot be configured during deserialization

  

如果一个元素具有一个具有相同名称的属性和子元素,则该属性似乎被忽略并且使用了元素值

     

目前无法处理同义词(属性或元素之间;或具有不同命名空间的属性之间)。