带列表的java Spring引导解组xml失败
我已经被这个问题困扰了一个多星期了,我正在尝试将Spring引导端点的@RequestBody中的application/xml解压到一个POJO。每个foo中的条列表无法解组shal和foo。条变为空。如果我将Foo中的“条列表”字段更改为一个条,我将得到每个Foo的最后一条。但是,Bar实例无法解组XmlValue条。价值b和Foo的顺序都已正确解组
我有这样一个xml:
<a>
<b>1</b>
<c>
<foo order="1">
<bar order="11">1</bar>
<bar order="22">2</bar>
<bar order="33">3</bar>
</foo>
<foo order="2">
<bar order="44">4</bar>
<bar order="55">5</bar>
<bar order="66">6</bar>
</foo>
</c>
</a>
以及以下POJO来解开它们:
酒吧。爪哇
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
@XmlAttribute(name = "order")
Integer number;
@XmlValue
Integer value;
public Bar() {}
public Bar(int number, int value){
this.number = number;
this.value = value;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
福。爪哇
public class Foo {
@XmlAttribute(name = "order")
Integer order;
@XmlElement(name = "bar")
List<Bar> bars;
public Foo() {}
public Foo(int order, List<Bar> bars){
this.order = order;
this.bars = bars;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public List<Bar> getBars() {
return bars;
}
public void setBars(List<Bar> bars) {
this.bars = bars;
}
}
身体。爪哇
@XmlRootElement(name = "a")
@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
Integer b;
List<Foo> c;
public Body(){
}
public Body(int b, List<Foo> c){
this.b = b;
this.c = c;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
public List<Foo> getC() {
return c;
}
public void setC(List<Foo> c) {
this.c = c;
}
}
我的终点是:
public Response postData(@RequestBody Body body) {
...
感谢您的帮助
# 1 楼答案
<a>
元素中<c>
和<foo>
XML元素的结构 与Body
类中的Java代码不匹配:有两种方法可以解决此问题
第一种选择(直截了当的方法):
在
Body
类中,将上面的行替换为(当然,还要相应地更改构造函数、getter和setter)。 此外,您需要创建另一个Java类
C
或者第二种选择(比上述方法简单得多,因为您不需要更改类结构):
在
Body
类中,将代码行替换为