使用 Jersey 解析子节点

发布于 2024-09-19 02:52:30 字数 1112 浏览 1 评论 0原文

我们正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-09-26 02:52:44

我认为您不能像这样用 XmlElement 进行注释。您可能需要创建一个单独的 SubXml 类;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="response")
public class OurResponse
{
    private String lang;
    private String amt;
    private String curr;
    private SubXml subXml;

    public OurResponse()
    {

    }

    //Getters and setters
}

public class SubXml
{
    private String authcode;

    public SubXml()
    {

    }

    public String getAuthcode()
    {
        return authcode;
    }

    public void setAuthcode(String authcode)
    {
        this.authcode = authcode;
    }
}

注意,您需要的唯一注释是 OurResponse 类上的 @XmlRootElement 并且您需要设置名称; 名称=“响应”

I don't think you can annotate with XmlElement like that. You might need to create a seperate SubXml class;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="response")
public class OurResponse
{
    private String lang;
    private String amt;
    private String curr;
    private SubXml subXml;

    public OurResponse()
    {

    }

    //Getters and setters
}

and

public class SubXml
{
    private String authcode;

    public SubXml()
    {

    }

    public String getAuthcode()
    {
        return authcode;
    }

    public void setAuthcode(String authcode)
    {
        this.authcode = authcode;
    }
}

Note that the only annotation you need is @XmlRootElement on the OurResponse class and you need to set the name; name="response".

无声情话 2024-09-26 02:52:41

您有几个不同的选择:

选项 1 - MOXy JAXB 和 MOXy JAXB @XmlPath

您可以使用 MOXy JAXB 实现和 @XmlPath 扩展要实现所需的结果:

import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlPath("subXml/authCode/text()")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

有关详细信息,请参阅:

选项 2 - 任何 JAXB Impl 和 @XmlJavaTypeAdapter

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlJavaTypeAdapter(AuthCodeAdapter.class)
    @XmlElement(name="subXml")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

以及

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AuthCodeAdapter extends XmlAdapter<SubXml, String> {

    @Override
    public String unmarshal(SubXml v) throws Exception {
        return v.getAuthCode();
    }

    @Override
    public SubXml marshal(String v) throws Exception {
        SubXml subXml = new SubXml();
        subXml.setAuthCode(v);
        return subXml;
    }

}

参阅

public class SubXml {

    private String authCode;

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

有关详细信息,请 :

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:

import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlPath("subXml/authCode/text()")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

For more information see:

Option 2 - Any JAXB Impl and @XmlJavaTypeAdapter

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="response")
public class OurResponse {
    private String authCode;

    @XmlJavaTypeAdapter(AuthCodeAdapter.class)
    @XmlElement(name="subXml")
    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

with

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class AuthCodeAdapter extends XmlAdapter<SubXml, String> {

    @Override
    public String unmarshal(SubXml v) throws Exception {
        return v.getAuthCode();
    }

    @Override
    public SubXml marshal(String v) throws Exception {
        SubXml subXml = new SubXml();
        subXml.setAuthCode(v);
        return subXml;
    }

}

and

public class SubXml {

    private String authCode;

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

}

For more information see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文