使用 Jersey 解析子节点
我们正在使用 Jersey 连接到第三方。然后我们要将返回的 xml 提取到我们的类中。除了 xml 中的一个节点位于子节点之外,这实际上工作正常。 下面是返回的 xml:
<response>
...
<langISO>en</langISO>
<acquirerAmount>1000</acquirerAmount>
<acquirerCurrency>GBP</acquirerCurrency>
<subXml>
<authCode>122958</authCode>
</subXml>
</response>
请注意,authCode 节点位于子节点(称为 subXml)中。
OurResponse myriadResponse = response.getEntity(OurResponse.class);
这是我们的类,但它没有解析出 authCode
package com.xxx;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Consumes("application/xml")
public class OurResponse {
private String authCode;
@XmlElement(name = "subXml/authCode")
public String getAuthCode() {
return authCode;
}
@XmlElement(name = "subXml/authCode")
public void setAuthCode(String authCode) {
this.authCode = authCode;
}
}
We are connecting to a third party using Jersey. We then want to extract the returned xml into our class. This is actually working fine except for one node in the xml that is in a subnode.
Here is the xml returned:
<response>
...
<langISO>en</langISO>
<acquirerAmount>1000</acquirerAmount>
<acquirerCurrency>GBP</acquirerCurrency>
<subXml>
<authCode>122958</authCode>
</subXml>
</response>
Note that the authCode node is in a subnode (called subXml).
OurResponse myriadResponse = response.getEntity(OurResponse.class);
Here is our class, but it is not parsing out the authCode
package com.xxx;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Consumes("application/xml")
public class OurResponse {
private String authCode;
@XmlElement(name = "subXml/authCode")
public String getAuthCode() {
return authCode;
}
@XmlElement(name = "subXml/authCode")
public void setAuthCode(String authCode) {
this.authCode = authCode;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不能像这样用 XmlElement 进行注释。您可能需要创建一个单独的 SubXml 类;
请
注意,您需要的唯一注释是 OurResponse 类上的
@XmlRootElement
并且您需要设置名称;名称=“响应”
。I don't think you can annotate with XmlElement like that. You might need to create a seperate SubXml class;
and
Note that the only annotation you need is
@XmlRootElement
on the OurResponse class and you need to set the name;name="response"
.您有几个不同的选择:
选项 1 - MOXy JAXB 和 MOXy JAXB @XmlPath
您可以使用 MOXy JAXB 实现和 @XmlPath 扩展要实现所需的结果:
有关详细信息,请参阅:
选项 2 - 任何 JAXB Impl 和 @XmlJavaTypeAdapter
以及
参阅
有关详细信息,请 :
You have a couple different options:
Option 1 - MOXy JAXB & @XmlPath
You could use the MOXy JAXB implementation and the @XmlPath extension to achieve the desired result:
For more information see:
Option 2 - Any JAXB Impl and @XmlJavaTypeAdapter
with
and
For more information see: