表示複雜類型的Java類可能是這樣的:
@XmlType(name = "demo")
public class Demo {
private String valueAttr;
private String valueContent;
@XmlAttribute(name = "value")
public String getValueAttr() {
return this.valueAttr;
}
public void setValueAttr(String valueAttr) {
this.valueAttr = valueAttr;
}
@XmlValue
public String getValueContent() {
return this.valueContent;
}
public void setValueContent(String valueContent) {
this.valueContent = valueContent;
}
}
類名,字段名,和任何你想他們是方法的名稱可以改變,因爲XML名稱在註釋中明確給出。
要看到它的工作,使用:
@XmlRootElement
public class Test {
@XmlElement
private Demo demo;
public static void main(String[] args) throws Exception {
Demo demo = new Demo();
demo.setValueAttr("this is the attr value");
demo.setValueContent("this is the element content");
Test test = new Test();
test.demo = demo;
JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(test, System.out);
}
}
輸出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<demo value="this is the attr value">this is the element content</demo>
</test>
現在用傑克遜API時春天來解讀呢?這是行不通的。我在POST中發送XML到春天休息控制器,它不填充任何東西。 – ProgrammerBoy
@ProgrammerBoy這應該起作用,所以如果沒有,那麼你做錯了什麼。既然你沒有顯示你所做的事情,我們不可能確定你做錯了什麼。這個問題已被回答。如果您還有其他問題,請創建一個新問題,幷包含所有相關信息,例如POST的實際有效負載,JABX註釋類以及處理POST的Spring MVC方法。 – Andreas
感謝您付出努力回答它......我很感激。我得到「JSON解析錯誤:無法識別的字段」「」錯誤信息。稍後我會針對細節創建一個單獨的問題。 – ProgrammerBoy