将名称空间设置为@XmlRootElement时,解组失败且没有错误 [英] Unmarshalling fails with no errors when setting namespace to @XmlRootElement
问题描述
我创建了一个 JAXB 对象,我正在尝试将xml字符串解组到其中。
我面临的问题是当我在 @XmlRootElement
中放入 namespace
属性时我发送的xml文档, JAXB 对象正在创建,但它是空的。如果我删除命名空间它的工作原理。所以这就是我的意思
I have created a JAXB object and I am trying to unmarshal an xml string into it.
The problem that I am facing is that when I put the namespace
property in the @XmlRootElement
and in the xml document that I am sending, the JAXB object is getting created but it is empty. If I remove the namespace it works. So here is what I mean
我的JAXB对象:
@XmlRootElement(name = "incident", namespace = "http://www.ba.com/schema/BAserviceDeskAPI/incident")
@XmlAccessorType(XmlAccessType.FIELD)
public class Incident {
@XmlElement
private String eventTitle;
public Incident() {
}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Incident [");
builder.append("eventTitle=");
builder.append(eventTitle);
builder.append("]");
return builder.toString();
}
}
我的主要
public static void main(String[] args) throws JAXBException {
String s = "<incident xmlns=\"http://www.ba.com/schema/BAserviceDeskAPI/incident\">"
+ "<eventTitle>Test Title from BAwrapper</eventTitle>"
+ "</incident>";
JAXBContext jaxbContext = JAXBContext.newInstance(Incident.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Incident incident = (Incident) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
System.out.println(incident.toString());
}
}
输出:
Incident [eventTitle=null]
如果我从<$ c $中删除,namespace =http://www.ba.com/schema/BAserviceDeskAPI/incident
c> @XmlRootElement 和 xmlns = \http://www.ba.com/schema/BAserviceDeskAPI/incident\
来自发送的xml我得到以下输出
If I remove the , namespace = "http://www.ba.com/schema/BAserviceDeskAPI/incident"
from the @XmlRootElement
and the xmlns=\"http://www.ba.com/schema/BAserviceDeskAPI/incident\"
from the xml sent I get the output below
事件[eventTitle =来自BAwrapper的测试标题]
为什么会出现这种情况?
Any ideas why this happens?
谢谢
推荐答案
@XmlRootElement
仅适用于该元素。如果您希望它应用于您映射到的所有元素,可以使用 @XmlSchema
注释在包级别执行此操作。
The namespace specified on @XmlRootElement
only applies to that element. If you want it to apply to all the elements you have mapped to, you can do it at the package level using the @XmlSchema
annotation.
package-info.java
@XmlSchema(
namespace = "http://www.ba.com/schema/BAserviceDeskAPI/incident",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
更多信息
我在博客上写了更多关于JAXB和名称空间限定的内容:
I have written more about JAXB and namespace qualification on my blog:
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
将命名空间设置为
@XmlRootElement
Unmarshalling fails with no errors when setting namespace to @XmlRootElement
对于JAXB我们(JSR-222专家组),解组失败且没有错误如果有未映射的内容,则默认情况下,unmarshal不会失败。为什么?因为很多XML文档包含额外的内容,所以事情总是会失败。如果您确实想看到这些错误,那么您可以在 Unmarshaller
上指定 ValidationEventHandler
。
For JAXB we (the JSR-222 expert group) decided that an unmarshal shouldn't fail by default if there is unmapped content. Why? Because alot of XML documents contain extra content and things would be failing all the time. If you do want to see these errors then you can specify a ValidationEventHandler
on the Unmarshaller
.
这篇关于将名称空间设置为@XmlRootElement时,解组失败且没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!