JAXBContext 和 @XmlNsForm 注释
请阐明 JAXBContext 配置。 给定:
- 客户库
com.mycompany.user01234
有几个 JAXB 注释的类 - 所有类都是位于同一包中的简单 POJO
- 类由
@XmlType
注释
客户将 com.mycompany.user01234.UserClass1.class
实例编组到服务器通过 Web 服务端点。在服务器端,我执行以下操作:
JAXBContext jbc = JAXBContext.newInstance("com.mycompany.user01234")
Unmarshaller um = jbc.createUnmarshaller();
JAXBElement<Object> element = um.unmarshal(source, Object.class);
Object customerInput = element.getValue();
仅如果我使用以下 package-info.java
修补客户库,它就可以正常工作:
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED)
package com.mycompany.user01234;
令我羞愧的是,我还没有找到任何明确的解释这个@XmlNsForm 注释是什么以及它如何影响解组过程。这是第一个问题。
第二个问题是是否可以(在给定的布局中)将该 QUALIFIED
值放入 JAXBContext
的某些属性或默认值中,或者使用其他非声明性方式允许获取摆脱package-info.java
。
非常感谢!
Please shed some light on JAXBContext configutation.
Given:
- customer library
com.mycompany.user01234
with several
JAXB-annotated classes - all classes are simple POJOs located in the same package
- classes are annotated by
@XmlType
Customer marshals instance of com.mycompany.user01234.UserClass1.class
to the server via web service endpoint. On the server side I do the following:
JAXBContext jbc = JAXBContext.newInstance("com.mycompany.user01234")
Unmarshaller um = jbc.createUnmarshaller();
JAXBElement<Object> element = um.unmarshal(source, Object.class);
Object customerInput = element.getValue();
And it works fine ONLY if I patch customer library with following package-info.java
:
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED)
package com.mycompany.user01234;
To my shame I havent found any clear explanation of what this @XmlNsForm
annotation is and how it affects unmarshalling process. This is the first question.
The second question is whether it is possible (in the given layout) to put that QUALIFIED
value into some properties or defaults for JAXBContext
or use other non-declarative means allowing to get rid of package-info.java
.
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它与
元素(即 XML 模式文档的顶级元素)的elementFormDefault
属性完全对应。它的作用(使用该常量)是声明模式定义的命名空间中的元素应该使用限定符呈现(即,作为
而不是
);两种样式之间声明名称空间的确切方式也会有所不同。就XML信息集而言,两种风格是完全等价的;如果在 XML 中正确声明了命名空间,JAXB 应该同样高兴(我相信它应该只在序列化时使用该值)。您可能想尝试通过传入您期望的类来创建您的
JAXBContext
,这样您就可以减少对发现代码的依赖(假设它是一个真正的FooBar
类)正在生成):(上面的代码是从我在代码测试套件中所做的事情中抽象出来的,这些事情肯定已经可以工作了。)
It corresponds exactly to the
elementFormDefault
attribute of an<xs:schema>
element (i.e., the top-level element of an XML Schema document). What it does (with that constant) is state that elements from the namespace defined by the schema should be rendered with a qualifier (i.e., as<foo:bar>
instead of<bar>
); the exact way that the namespace is declared will also vary between the two styles. In terms of the XML infoset, the two styles are completely equivalent; if namespaces are declared correctly in the XML, JAXB should be equally happy (I believe it should only use the value when serializing).You might want to try making your
JAXBContext
by passing in the class that you are expecting so that you are a little less reliant on discovery code (assuming it's aFooBar
class that's really being produced):(The above code is abstracted from things that I do in my code's test suite that definitely already work.)