尝试反序列化 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>
和 ScheduleDto 到
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScheduleDto {
@JsonProperty(value = "DatesObj")
@JacksonXmlElementWrapper(useWrapping = false)
private List<DatesDto> dates = new ArrayList<>();
}
它工作正常。
这是 jackson 错误吗?因为我期望它将被递归处理,所以在内部 <Dates>
的反序列化期间它不会知道有关外部的任何信息 <Dates>
。
或者它可以以某种方式配置为正确反序列化原始 XML(因为我无法更改输入)?
最佳答案
看起来像是一个错误。这可能是问题(或此问题的重复问题之一):https://github.com/FasterXML/jackson-dataformat-xml/issues/294
可能会在2.11.1版本中修复。这就是我找到的全部内容。
关于java - "MismatchedInputException: Cannot deserialize instance of ` java.lang.String ` out of START_OBJECT token"当内部标签与外部同名时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59726244/