java-如何使用jaxb读取属性?
作者:互联网
鉴于此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;
}
}
标签:xml-deserialization,jaxb,spring,java 来源: https://codeday.me/bug/20191122/2063404.html