开发者

JAXBContext and @XmlNsForm annotation

开发者 https://www.devze.com 2023-04-02 12:04 出处:网络
Please shed some light on JAXBContext configutation. Given: customer library com.mycompany.user01234 with several

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开发者_StackOverflow社区 @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!


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 a FooBar class that's really being produced):

JAXBContext jbc = JAXBContext.newInstance(FooBar.class);
FooBar customerInput = (FooBar) jbc.createUnmarshaller().unmarshal(source);

(The above code is abstracted from things that I do in my code's test suite that definitely already work.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号