简体   繁体   English

当有 2 个命名空间时,如何使用 JAXB 解组对 2 个 java 对象的 XML 响应?

[英]How can I unmarshal an XML response to 2 java objects using JAXB when there are 2 namespaces?

Thank you for taking the time to read.感谢您花时间阅读。

My goal is to deserialize the response from an API request into 2 usable java objects.我的目标是将来自 API 请求的响应反序列化为 2 个可用的 java 对象。

I am sending an POST request to an endpoint to create a job in our schedule.我正在向端点发送一个 POST 请求,以在我们的计划中创建一个作业。 The job is created successfully and the following XML is returned in the body:作业创建成功,正文中返回以下 XML:

<entry xmlns="http://purl.org/atom/ns#">
    <id>0</id>
    <title>Job has been created.</title>
    <source>com.tidalsoft.framework.rpc.Result</source>
    <tes:result xmlns:tes="http://www.auto-schedule.com/client">
        <tes:message>Job has been created.</tes:message>
        <tes:objectid>42320</tes:objectid>
        <tes:id>0</tes:id>
        <tes:operation>CREATE</tes:operation>
        <tes:ok>true</tes:ok>
        <tes:objectname>Job</tes:objectname>
    </tes:result>
</entry>

When I attempt to only capture the elements id , title , and source the data is captured successfully.当我尝试仅捕获元素idtitlesource时,数据被成功捕获。 The problem is when I introduce the child object, which attempts to capture the data in the tes:result element.问题是当我引入子 object 时,它试图捕获tes:result元素中的数据。

Here's what the parent POJO looks like:这是父 POJO 的样子:

@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

    private String id;

    private String title;

    private String source;

    ResponseDetails result;

    public Response() {}
}

and here is the child object:这是子 object:

@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseDetails {

    @XmlElement(name = "tes:message")
    String message;
    
    @XmlElement(name = "tes:objectid")
    String objectid;

    @XmlElement(name = "tes:operation")
    String operation;

    @XmlElement(name = "tes:ok")
    String ok;

    @XmlElement(name = "tes:objectname")
    String objectname;
}

Finally, here is the package-info.java file I am using:最后,这是我正在使用的 package-info.java 文件:

@XmlSchema(
        namespace = "http://purl.org/atom/ns#",
        elementFormDefault = XmlNsForm.QUALIFIED)
package com.netspend.raven.tidal.request.response;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Any ideas are greatly appreciated.任何想法都非常感谢。 Thanks.谢谢。

The problem is: Your Java code doesn't correctly specify the namespaces corresponding to the inner XML element问题是:您的 Java 代码未正确指定与内部 XML 元素对应的命名空间

<tes:result xmlns:tes="http://www.auto-schedule.com/client">
    <tes:message>Job has been created.</tes:message>
    <tes:objectid>42320</tes:objectid>
    <tes:id>0</tes:id>
    <tes:operation>CREATE</tes:operation>
    <tes:ok>true</tes:ok>
    <tes:objectname>Job</tes:objectname>
</tes:result>

The <tes:result> XML element has the namespace "http://www.auto-schedule.com/client" . <tes:result> XML 元素具有命名空间"http://www.auto-schedule.com/client" The namespace prefix tes: itself is irrelevant for Java.命名空间前缀tes:本身与 Java 无关。 (XML namespace prefixes were invented only for better readability of the XML code.) Therefore in your Response class, instead of just writing (发明 XML 命名空间前缀只是为了提高 XML 代码的可读性。)因此在您的Response class 中,而不是仅仅写

ResponseDetails result;

you need to write你需要写

@XmlElement(namespace = "http://www.auto-schedule.com/client")
ResponseDetails result;

See also the javadoc of XmlElement.namespace .另请参见XmlElement.namespace的 javadoc。

Also all the XML sub-elements within the <tes:result> are specified with this namespace "http://www.auto-schedule.com/client" .此外, <tes:result>中的所有 XML 子元素都使用此命名空间"http://www.auto-schedule.com/client"指定。 Therefore, also inside your ResponseDetails you have to correct the namespace stuff.因此,在您的ResponseDetails中,您还必须更正命名空间的内容。 For example, instead of例如,而不是

@XmlElement(name = "tes:message")
String message;

you need to write你需要写

@XmlElement(name = "message", namespace = "http://www.auto-schedule.com/client")
String message;

You may also omit the name = "message" and simply write您也可以省略name = "message"并简单地写

@XmlElement(namespace = "http://www.auto-schedule.com/client")
String message;

because JAXB picks up this name from your Java property name message .因为 JAXB 从您的 Java 属性名称message中获取此名称。

And similar for all the other properties in this class.与此 class 中的所有其他属性类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM