当内部标记与外部标记同名时"MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of ST



尝试反序列化 XML 时

<Schedule>
<Dates>
<Calendar>USD</Calendar>
<Convention>ModifiedFollowing</Convention>
<Tenor>1M</Tenor>
<Dates>
<Date>2012-01-06</Date>
<Date>2012-04-10</Date>
<Date>2012-07-06</Date>
<Date>2012-10-08</Date>
<Date>2013-01-07</Date>
<Date>2013-04-08</Date>
</Dates>
</Dates>
</Schedule>

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScheduleDto {
@JsonProperty(value = "Dates")
@JacksonXmlElementWrapper(useWrapping = false)
private List<DatesDto> dates = new ArrayList<>();
}
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "Dates")
public class DatesDto {
@JsonProperty(value = "Calendar")
private String calendar;
@JsonProperty(value = "Convention")
private String convention;
@JsonProperty(value = "Tenor")
private String tenor;
@JsonProperty(value = "Date")
@JacksonXmlElementWrapper(localName = "Dates")
private List<String> dates = new ArrayList<>();
}

它抛出一个错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (StringReader); line: 1, column: 108] (through reference chain: sh.zandr.sandbox.how2jackson.domain.ScheduleDto["Dates"]->java.util.ArrayList[0]->sh.zandr.sandbox.how2jackson.domain.DatesDto["Dates"]->java.util.ArrayList[0])

但是如果我将 xml 更改为

<Schedule>
<DatesObj>
<Calendar>USD</Calendar>
<Convention>ModifiedFollowing</Convention>
<Tenor>1M</Tenor>
<Dates>
<Date>2012-01-06</Date>
<Date>2012-04-10</Date>
<Date>2012-07-06</Date>
<Date>2012-10-08</Date>
<Date>2013-01-07</Date>
<Date>2013-04-08</Date>
</Dates>
</DatesObj>
</Schedule>

和计划到

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScheduleDto {
@JsonProperty(value = "DatesObj")
@JacksonXmlElementWrapper(useWrapping = false)
private List<DatesDto> dates = new ArrayList<>();
}

它工作正常。

是杰克逊虫吗?因为我期望它会递归处理,所以在内部<Dates>的反序列化过程中,它不会知道外部<Dates>的任何信息。 或者可以以某种方式将其配置为正确反序列化原始 XML(因为我无法更改输入(?

它看起来像一个错误。这可能是问题所在(或此问题的副本之一(:https://github.com/FasterXML/jackson-dataformat-xml/issues/294

它可能在版本 2.11.1 中修复。这就是我发现的全部内容。

相关内容

最新更新