表示复杂类型的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