1
我有下面的XML,我想反序列化(簡稱樣品):傑克遜XML序列化,多態映射
<annots>
<highlight subject="Highlight" page="0">
<contents>askdhaskjdhaskjhsakjhdakjshd</contents>
</highlight>
<text subject="Comment" page="0" title="Guest" color="#FFFF00">
<contents>Memo</contents>
</text>
</annots>
我想出了到目前爲止的代碼:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "subject")
@JsonSubTypes({
@JsonSubTypes.Type(name = "Highlight", value = Highlight.class),
@JsonSubTypes.Type(name = "Comment", value = Text.class)
})
public abstract class Annotation {
@JacksonXmlProperty(localName = "subject")
public String subject;
@JacksonXmlProperty(isAttribute = true,localName = "page")
public String page;
@JacksonXmlProperty(isAttribute = true,localName = "rect")
public String rect;
@JacksonXmlProperty(isAttribute = true,localName = "flags")
public String flags;
@JacksonXmlProperty(isAttribute = true,localName = "name")
public String name;
@JacksonXmlProperty(isAttribute = true,localName = "title")
public String title;
@JacksonXmlProperty(isAttribute = true,localName = "color")
public String color;
@JacksonXmlProperty(isAttribute = true,localName = "opacity")
public String opacity;
@JacksonXmlProperty(localName = "contents")
public String contents;
}
但有這種方法有幾個問題:
我不想爲@JsonTypeInfo使用「主題」。相反,我想使用標籤名稱(高亮,文字,...)。我怎樣才能從XML中得到這個?
@JacksonXmlPropert(localName =「subject」)始終爲空,因爲我將它用於類型信息。爲什麼?或者如何避開它?
或者是方法已經錯了?
最後周圍的I類使用:
@JacksonXmlElementWrapper(localName = "annots")
@JacksonXmlProperty(localName = "annots")
public List<Annotation> annots;