Hi Guy,
XML namespaces should be supported by the JAXB based natural notation,
even if there is no namespace URI information conveyed as part of the
JSON data.
JAXB RI provides expected tag information, and Jersey tries to restore
the namespace
data out of it before generating appropriate StAX events.
You can imagine that the above approach could fail in some cases.
From what you wrote, i am not sure if you are hitting such a corner case
or if there is a bug somewhere in Jersey/JAXB.
You may want to try if the Jackson based POJO binding (see [1])
works better for you. Regardless if that works or not, please consider
sending a small reproducible test case so that we can investigate further.
It might also be a small configuration glitch.
Thanks,
~Jakub
[1]
http://jersey.java.net/nonav/documentation/latest/json.html#d4e903
On 28.9.2011 6:39, Guy Wens wrote:
> Hi,
>
> I am currently looking to add support for the natural JSON notation
> for existing JAXB annotated classes. These classes are in a package
> containing a package-info.java to declare an XML namespace.
>
> As a test I added a package-info.java class to the Jersey example
> https://maven.java.net/service/local/artifact/maven/redirect?r=releases&g=com.sun.jersey.samples&a=json-from-jaxb&v=1.9.1&c=project&e=zip
> <https://maven.java.net/service/local/artifact/maven/redirect?r=releases&g=com.sun.jersey.samples&a=json-from-jaxb&v=1.9.1&c=project&e=zip>,
> which contains the following:
>
>
> @XmlSchema(namespace = "http://www.example.com/test",
> elementFormDefault = XmlNsForm.QUALIFIED)
> package com.sun.jersey.samples.jsonfromjaxb.jaxb;
>
> import javax.xml.bind.annotation.XmlNsForm;
> import javax.xml.bind.annotation.XmlSchema;
>
>
> When running the XML (de-)serialization tests all is fine:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?><flights
> xmlns="http://www.example.com/test"><flight><flightId>OK123</flightId><company>Czech
> Airlines</company><number>123</number><aircraft>B737</aircraft></flight><flight><flightId>OK124</flightId><company>Czech
> Airlines</company><number>124</number><aircraft>AB115</aircraft></flight></flights>
>
>
>
> But for the JSON format tests I get the following error:
>
> testGetOnAircraftsJSONFormat(com.sun.jersey.samples.jsonfromjaxb.MainTest)
> Time elapsed: 0.641 sec <<< ERROR!
> javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
> - with linked exception:
> [javax.xml.bind.UnmarshalException: unexpected element (uri:"",
> local:"aircraftType"). Expected elements are
> <{http://www.example.com/test}aircraftType>,<{http://www.example.com/test}flights>]
> at
> com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:314)
> at
> com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
> at
> com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523)
> at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
> at
> com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
> at
> com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:508)
> at
> com.sun.jersey.samples.jsonfromjaxb.MainTest.testGetOnAircraftsJSONFormat(MainTest.java:202)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
> at
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
> at
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
> at
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
> at
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
> at
> org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
> at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
> at
> org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
> at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
> at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
> at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
> at
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
> Caused by: javax.xml.bind.UnmarshalException
> - with linked exception:
> [javax.xml.bind.UnmarshalException: unexpected element (uri:"",
> local:"aircraftType"). Expected elements are
> <{http://www.example.com/test}aircraftType>,<{http://www.example.com/test}flights>]
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:431)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:368)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:338)
> at
> com.sun.jersey.json.impl.JSONUnmarshallerImpl.unmarshal(JSONUnmarshallerImpl.java:113)
> at
> com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:295)
> ... 35 more
> Caused by: javax.xml.bind.UnmarshalException: unexpected element
> (uri:"", local:"aircraftType"). Expected elements are
> <{http://www.example.com/test}aircraftType>,<{http://www.example.com/test}flights>
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1052)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:464)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:247)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:181)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:366)
> ... 38 more
> Caused by: javax.xml.bind.UnmarshalException: unexpected element
> (uri:"", local:"aircraftType"). Expected elements are
> <{http://www.example.com/test}aircraftType>,<{http://www.example.com/test}flights>
> ... 49 more
>
>
> Is this supposed to work or can the natural notation not be used when
> there is a namespace declared?
>
> Thanks,
> Guy
>