鉴于此 XML:
<response>
<detail Id="123" Length="10" Width="20" Height="30" />
</response>
这就是我现在拥有的,但它不起作用(我得到的结果是空的):
@XmlRootElement(name="response")
public class MyResponse {
List<ResponseDetail> response;
//+getters +setters +constructor
}
public class MyResponseDetail {
Integer Id;
Integer Length;
Integer Width;
Integer Height;
//+getters +setters
}
我正在使用 RestOperations
调用远程服务我想解析 <detail ..>
元素。我试过同时通过 MyResponse
和 MyResponseDetail
类 RestOperations
但结果总是空的。
我的对象结构应该是什么样子才能匹配该 XML?
最佳答案
你需要这样注释你的类:
@XmlRootElement
public class Response {
private List<Detail> detail;
public void setDetail(List<Detail> detail) {
this.detail = detail;
}
public List<Detail> getDetail() {
return detail;
}
}
public class Detail {
private String id;
/* add other attributes here */
@XmlAttribute(name = "Id")
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
关于java - 如何使用 jaxb 读取属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232795/