当有 2 个命名空间时,如何使用 JAXB 解组对 2 java 对象的 XML 响应?
How can I unmarshal an XML response to 2 java objects using JAXB when there are 2 namespaces?
感谢您抽空阅读。
我的目标是将 API 请求的响应反序列化为 2 个可用的 java 对象。
我正在向端点发送 POST 请求以在我们的日程安排中创建作业。 job创建成功,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>
当我尝试仅捕获元素 id、title 和 source 时数据抓取成功。问题是当我引入子对象时,它试图捕获 tes:result 元素中的数据。
这是父 POJO 的样子:
@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
private String id;
private String title;
private String source;
ResponseDetails result;
public Response() {}
}
这是子对象:
@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;
}
最后,这是我正在使用的包-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;
非常感谢任何想法。谢谢。
问题是:您的 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>
<tes:result>
XML 元素具有命名空间 "http://www.auto-schedule.com/client"
。
名称空间前缀 tes:
本身与 Java 无关。
(XML 命名空间前缀的发明只是为了提高 XML 代码的可读性。)
因此在你的 Response
class 中,而不是只写
ResponseDetails result;
你需要写
@XmlElement(namespace = "http://www.auto-schedule.com/client")
ResponseDetails result;
另请参阅 XmlElement.namespace
的 javadoc。
还指定了 <tes:result>
中的所有 XML 子元素
使用此命名空间 "http://www.auto-schedule.com/client"
。
因此,也在你的 ResponseDetails
里面你必须更正
命名空间的东西。例如,而不是
@XmlElement(name = "tes:message")
String message;
你需要写
@XmlElement(name = "message", namespace = "http://www.auto-schedule.com/client")
String message;
您也可以省略 name = "message"
并简单地写
@XmlElement(namespace = "http://www.auto-schedule.com/client")
String message;
因为 JAXB 从您的 Java 属性 名称 message
.
中获取此名称
与此 class 中的所有其他属性类似。
感谢您抽空阅读。
我的目标是将 API 请求的响应反序列化为 2 个可用的 java 对象。
我正在向端点发送 POST 请求以在我们的日程安排中创建作业。 job创建成功,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>
当我尝试仅捕获元素 id、title 和 source 时数据抓取成功。问题是当我引入子对象时,它试图捕获 tes:result 元素中的数据。
这是父 POJO 的样子:
@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
private String id;
private String title;
private String source;
ResponseDetails result;
public Response() {}
}
这是子对象:
@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;
}
最后,这是我正在使用的包-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;
非常感谢任何想法。谢谢。
问题是:您的 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>
<tes:result>
XML 元素具有命名空间 "http://www.auto-schedule.com/client"
。
名称空间前缀 tes:
本身与 Java 无关。
(XML 命名空间前缀的发明只是为了提高 XML 代码的可读性。)
因此在你的 Response
class 中,而不是只写
ResponseDetails result;
你需要写
@XmlElement(namespace = "http://www.auto-schedule.com/client")
ResponseDetails result;
另请参阅 XmlElement.namespace
的 javadoc。
还指定了 <tes:result>
中的所有 XML 子元素
使用此命名空间 "http://www.auto-schedule.com/client"
。
因此,也在你的 ResponseDetails
里面你必须更正
命名空间的东西。例如,而不是
@XmlElement(name = "tes:message")
String message;
你需要写
@XmlElement(name = "message", namespace = "http://www.auto-schedule.com/client")
String message;
您也可以省略 name = "message"
并简单地写
@XmlElement(namespace = "http://www.auto-schedule.com/client")
String message;
因为 JAXB 从您的 Java 属性 名称 message
.
与此 class 中的所有其他属性类似。