users@jersey.java.net

[Jersey] UnmarshalException for natural JSON format and namespace

From: <guy.wens_at_lumacentral.com>
Date: Wed, 28 Sep 2011 13:45:38 +0000 (GMT)

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=release
s&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></fligh
t></flights>


But for the JSON format tests I get the following error:

testGetOnAircraftsJSONFormat(com.sun.jersey.samples.jsonfromjaxb.MainTe
st) 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/te
st}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.testGetOnAircraftsJSONForm
at(MainTest.java:202)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.ja
va:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMe
thod.java:44)
    at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCalla
ble.java:15)
    at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMeth
od.java:41)
    at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMetho
d.java:20)
    at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.ja
va:28)
    at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java
:31)
    at
org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4Class
Runner.java:79)
    at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunne
r.java:71)
    at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunne
r.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.ja
va:59)
    at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestS
et(AbstractDirectoryTestSuite.java:115)
    at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(Abst
ractDirectoryTestSuite.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.ja
va:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(Sure
fireBooter.java:350)
    at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.jav
a: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/te
st}flights>]
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamE
xception(UnmarshallerImpl.java:431)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Un
marshallerImpl.java:368)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unm
arshallerImpl.java:338)
    at
com.sun.jersey.json.impl.JSONUnmarshallerImpl.unmarshal(JSONUnmarshalle
rImpl.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/te
st}flights>
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEve
nt(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.reportUnexpectedChildEl
ement(Loader.java:120)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRo
otLoader.childElement(UnmarshallingContext.java:1052)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startEle
ment(UnmarshallingContext.java:483)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElem
ent(UnmarshallingContext.java:464)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startEleme
nt(InterningXmlVisitor.java:75)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStar
tElement(StAXStreamConnector.java:247)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StA
XStreamConnector.java:181)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Un
marshallerImpl.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/te
st}flights>
    ... 49 more


Is this supposed to work or can the natural notation not be used when
there is a namespace declared?

Thanks,
Guy