我实现了一些后端,我正在尝试与使用XML作为传输的rest服务器集成。有一个Spring使用JAXB自动处理XML解析。但是来自此服务器的一些请求和响应具有相同名称的标记。例如:
<transaction type="reserve">
<date>2014-12-11</date>
<station-from>London</station-from>
<station-to>Paris</station-to>
<place>1</place>
<detail>
<transaction id-seller="123">
<some-tag>bla-bla</some-tag>
</transaction>
</detail>
</transaction>
Java代码:
@XmlRootElement(name = "transaction")
public class Transaction {
private String date;
private String stationFrom;
private String stationTo;
private int place;
private Detail detail;
public Transaction() {}
@XmlElement(name = "date")
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@XmlElement(name = "station-from")
public String getStationFrom() {
return stationFrom;
}
public void setStationFrom(String stationFrom) {
this.stationFrom = stationFrom;
}
@XmlElement(name = "station-to")
public String getStationTo() {
return stationTo;
}
public void setStationTo(String stationTo) {
this.stationTo = stationTo;
}
@XmlElement(name = "place")
public int getPlace() {
return place;
}
public void setPlace(int place) {
this.place = place;
}
@XmlRootElement(name = "detail")
public static class Detail {
private Transaction transaction;
public Detail() {}
@XmlElement(name = "transaction")
public Transaction getTransaction() {
return transaction;
}
public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}
@XmlRootElement(name = "transaction")
public static class DetailTransaction {
private String idSeller;
private String someTag;
public DetailTransaction() {}
@XmlAttribute(name = "id-seller")
public String getIdSeller() {
return idSeller;
}
public void setIdSeller(String idSeller) {
this.idSeller = idSeller;
}
@XmlAttribute(name = "some-tag")
public String getSomeTag() {
return someTag;
}
public void setSomeTag(String someTag) {
this.someTag = someTag;
}
}
}
}
有两个标签名称相同 - 交易。我如何通过JAXB处理它?顺便提一下,我没有XSD架构,这项服务不提供它。在tomcat中我得到了这个例外:
Two classes have the same XML type name "transaction". Use @XmlType.name and @XmlType.namespace to assign different names to them.
实际上这个例子与我从这个REST服务器获得的那些复杂的响应无关,但是这个想法是有很多地方他们使用具有相同名称但在不同地方和不同级别的标签。
答案 0 :(得分:0)
我看到以下问题:
@XmlRootElement
。Detail
使用Transaction
,而非DetailTransaction
。Transaction
错过了transaction
。尝试以下方法:
@XmlRootElement(name = "transaction")
public class Transaction {
private String date;
private String stationFrom;
private String stationTo;
private int place;
private Detail detail;
public Transaction() {}
@XmlElement(name = "date")
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@XmlElement(name = "station-from")
public String getStationFrom() {
return stationFrom;
}
public void setStationFrom(String stationFrom) {
this.stationFrom = stationFrom;
}
@XmlElement(name = "station-to")
public String getStationTo() {
return stationTo;
}
public void setStationTo(String stationTo) {
this.stationTo = stationTo;
}
@XmlElement(name = "place")
public int getPlace() {
return place;
}
public void setPlace(int place) {
this.place = place;
}
@XmlElement(name = "detail")
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
public static class Detail {
private DetailTransaction transaction;
public Detail() {}
@XmlElement(name = "transaction")
public DetailTransaction getTransaction() {
return transaction;
}
public void setTransaction(DetailTransaction transaction) {
this.transaction = transaction;
}
public static class DetailTransaction {
private String idSeller;
private String someTag;
public DetailTransaction() {}
@XmlAttribute(name = "id-seller")
public String getIdSeller() {
return idSeller;
}
public void setIdSeller(String idSeller) {
this.idSeller = idSeller;
}
@XmlAttribute(name = "some-tag")
public String getSomeTag() {
return someTag;
}
public void setSomeTag(String someTag) {
this.someTag = someTag;
}
}
}
}
未经测试,只是破解了你的代码。在使用真实服务进行测试之前,进行单元测试(实例化上下文,解组样本XML,编组样本对象)。