JAXB Unmarshalling Exceptions

1,239 views
Skip to first unread message

John Goodsen

unread,
Aug 31, 2010, 3:10:20 PM8/31/10
to play-framework
I have some code that is unmarshalling some XML using JAXB. When I
run my junit test, it works fine. When I try it inside play, I"m
getting exceptions unmarshalling the XML. The only thing I can think
of is that its a classloader issue. In my testcase, I'm getting a Sun
classloader. Inside the Play app, I'm getting Play.classloader

I'm using this.getClass().getClassloader() to get the loader and
passing that into the newInstanc() method like this:

JAXBContext jc = JAXBContext.newInstance(jaxbContext,
Play.classloader);
Unmarshaller u = jc.createUnmarshaller();
return u.unmarshal(source);

The exception is a java.xml.bind.UnmarshalException with the message
"unexpected element" - I cannot print the XML on a public form - as
it's proprietary ...

thanks in advance for any ideas,

John

Julien Tournay

unread,
Aug 31, 2010, 3:59:40 PM8/31/10
to play-fr...@googlegroups.com
Doesn't sounds like a classloader issue to me since you get the error when unmarshalling, so you do have an Unmarshaller instance.
Are the parsed XML exactly the same in your test and in Play! ? Because the Exception indicate that JAXB found something and does not know how to deal with. Maybe an encoding issue ? You absolutely can't post the XML? It's really hard to help without that.

Julien.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
Real Programmers don't need comments-- the code is obvious.

John Goodsen

unread,
Aug 31, 2010, 10:10:10 PM8/31/10
to play-framework
OK, I've sanitized the urls and stuff so it won't disclose the project
I'm working on.

The only thing different that I can think of might be classpaths and
classloaders. Thanks in advance for any help you can provide...

Here's a JUnit test that passes when it runs stand-alone:

@Test
public void unmarshallStateXml() throws Exception {

String data = <... the XML I'm processing - see it posted
below ...>

ByteArrayInputStream source = new
ByteArrayInputStream(data.getBytes());

source.reset();
JAXBContext jc =
JAXBContext.newInstance("com.radsoft.client.wsclasses");
Unmarshaller u = jc.createUnmarshaller();
Object obj = u.unmarshal(source);
assertTrue(obj.toString().startsWith("ArrayOfSetting"));
}


I put the exact same code above running into a Play controller
method. When I hit the Controller action from a web browser, I get
this exception:

Java exception

RuntimeException occured : javax.xml.bind.UnmarshalException:
unexpected element (uri:"http://xml.radsoft.com/services",
local:"ArrayOfSetting"). Expected elements are <{http://
schemas.datacontract.org/2004/07/BusinessObjects.Common}
AccountStatus>,<{http://schemas.datacontract.org/2004/07/
BusinessObjects.Common}AccountType>,<{}Address>,<{http://
xml.radsoft.com/services}Address>,<{http://schemas.datacontract.org/
2004/07/BusinessObjects.Common}AddressType>,<{}
AddressValidation>,<{http://xml.radsoft.com/services}
AddressValidation>,<{}ArrayOfAddress>,<{http://xml.radsoft.com/
services}ArrayOfAddress>,<{}ArrayOfProductCategory>,<{http://
xml.radsoft.com/services}ArrayOfProductCategory>,<{}
ArrayOfProductContributor>,<{http://xml.radsoft.com/services}
ArrayOfProductContributor>,<{}ArrayOfProductReview>,<{http://
xml.radsoft.com/services}ArrayOfProductReview>,<{}
ArrayOfProductRightsTerritory>,<{http://xml.radsoft.com/services}
ArrayOfProductRightsTerritory>,<{}ArrayOfProductSubmission>,<{http://
xml.radsoft.com/services}ArrayOfProductSubmission>,<{}
ArrayOfSetting>,<{http://schemas.datacontract.org/2004/07/
BusinessObjects.SettingsDataMgr}ArrayOfSetting>,<{}Contact>,<{http://
xml.radsoft.com/services}Contact>,<{http://xml.radsoft.com/services}
FileConversionStatus>,<{}FileConvertData>,<{http://xml.radsoft.com/
services}FileConvertData>,<{}FinancialInfo>,<{http://xml.radsoft.com/
services}FinancialInfo>,<{http://schemas.datacontract.org/2004/07/
BusinessObjects.Common}GovtIdType>,<{http://xml.radsoft.com/services}
ImageConversionStatus>,<{}Invite>,<{http://xml.radsoft.com/services}
Invite>,<{}ProductCategory>,<{http://xml.radsoft.com/services}
ProductCategory>,<{}ProductContributor>,<{http://xml.radsoft.com/
services}ProductContributor>,<{}ProductReview>,<{http://
xml.radsoft.com/services}ProductReview>,<{}
ProductRightsTerritory>,<{http://xml.radsoft.com/services}
ProductRightsTerritory>,<{}ProductSubmission>,<{http://xml.radsoft.com/
services}ProductSubmission>,<{}PubAgreement>,<{http://xml.radsoft.com/
services}PubAgreement>,<{}PubItResponse>,<{http://xml.radsoft.com/
services}PubItResponse>,<{}Publisher>,<{http://xml.radsoft.com/
services}Publisher>,<{}Setting>,<{http://schemas.datacontract.org/
2004/07/BusinessObjects.SettingsDataMgr}Setting>,<{}
StringData>,<{http://xml.radsoft.com/services}StringData>,<{http://
xml.radsoft.com/services}SubmissionStatus>

Finally, here's the XML I'm processing

<?xml version="1.0"?>
<ArrayOfSetting xmlns="http://xml.radsoft.com/services"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Setting>
<SettingId>AL</SettingId>
<SettingValue>Alabama</SettingValue>
</Setting>
<Setting>
<SettingId>AK</SettingId>
<SettingValue>Alaska</SettingValue>
</Setting>
<Setting>
<SettingId>AS</SettingId>
<SettingValue>American Samoa</SettingValue>
</Setting>
<Setting>
<SettingId>WI</SettingId>
<SettingValue>Wisconsin</SettingValue>
</Setting>
<Setting>
<SettingId>WY</SettingId>
<SettingValue>Wyoming</SettingValue>
</Setting>
</ArrayOfSetting>


Julien Tournay

unread,
Sep 1, 2010, 4:01:48 AM9/1/10
to play-fr...@googlegroups.com
Do you use annotation or an xsd file to configure xml binding ?
If it's an xsd, where did you put it in your play app ?

Julien



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

John Goodsen

unread,
Sep 1, 2010, 2:52:10 PM9/1/10
to play-framework
looks like annotations on the files
> > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go oglegroups.com>
> > .

John Goodsen

unread,
Sep 1, 2010, 3:04:56 PM9/1/10
to play-framework
I still have to believe it has something to do with the Play
classloader and classpaths. I'm curious, is ANYBODY out there using
JAXB Web Services inside a Play application and successfully
marshalling/unmarshalling JAXB Annotated classes from/to XML ?

The differences (that I can tell) between the environments of my
standalone junit test (where the JAXB marshalling works) and calling
that same test from the play framework (where it doesn't work) is:

- different classloader - the unit test uses the default
classloader, the Play app uses the Play classloader.
- different classpaths - a ton of jars that are included from the
play/framework/lib directory. I've trimmed the unit test case down to
no external classpaths - it's getting all of it's JAXB libraries from
the Java 6 Runtime. I wonder if there's some kind of conflict in the
play/frameworki/lib jars and the JVM jars, since JAXB XML libraries
have been moved to the Java Runtime in Java 6 ?

BTW, I'm running on Mac OS X 10.6.4 and using the JavaVM 1.6.0 .

I'm trying to port a crappyily built Java webapp to Play! and show
this team a better way of life, but this XML parsing will be a
showstopper. I really hate to see this XML marshalling be a
showstopper.

thanks for any help,


GrailsDeveloper

unread,
Sep 1, 2010, 3:15:38 PM9/1/10
to play-framework
Two thoughts, which could help to analyze the problem:

1. Create a small example app which demonstrate the problem. Makes it
as small as possible and publish it.

2. Add all Play-jars to the unit-test classpath and see if you will
get then the same trouble.

Niels

Julien Tournay

unread,
Sep 1, 2010, 3:26:52 PM9/1/10
to play-fr...@googlegroups.com
Humf, that's weird.

The error is REALLY strange because it says that JAXB found an unexpected "ArrayOfSetting" and gives you "ArrayOfSetting" in the expected elements list.
This list tend to prove that JAXB did find your classes, but smth failed while parsing your XML.
I'll try to create a minimal app to see if it works for me.

Julien.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

John Goodsen

unread,
Sep 1, 2010, 4:22:52 PM9/1/10
to play-framework
The test case is in the play project. I just added all the play/
framework/lib jars and ran the tests (inside IntelliJ) and they still
pass.

I'm working on a small example app that I can publish...

thanks for the suggestions

On Sep 1, 3:15 pm, GrailsDeveloper <opensourc...@googlemail.com>
wrote:

Julien Tournay

unread,
Sep 1, 2010, 5:11:13 PM9/1/10
to play-fr...@googlegroups.com
Let's do that.

Julien.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

John Goodsen

unread,
Sep 1, 2010, 5:49:26 PM9/1/10
to play-framework
OK! I've it down the simplest barebones app I could produce - 2 JAXB-
WS classes to unmarshall, the XML file and a Play controller that
exposes the defect when it runs in Play.

I put the project up at github where you can clone it or download the
sources. Here's the URL http://github.com/jgoodsen/jaxbtest

Thanks for looking at this and good luck with figuring it out.
> > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go oglegroups.com>
> > .

Julien Tournay

unread,
Sep 2, 2010, 3:30:10 AM9/2/10
to play-fr...@googlegroups.com
Do you use annotations or a xsd to configure binding?

Julien Tournay

unread,
Sep 2, 2010, 4:52:40 AM9/2/10
to play-fr...@googlegroups.com
Opps sorry for double posting, iPhone problem....

I've tested it, it fails with play 1.0.x, but works with 1.1.
Can you use 1.1 ?

Julien.

Guillaume Bort

unread,
Sep 2, 2010, 5:57:29 AM9/2/10
to play-fr...@googlegroups.com
Yes same here. Don't know why but it works with play 1.1-beta1

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Guillaume Bort

unread,
Sep 2, 2010, 8:31:51 AM9/2/10
to play-fr...@googlegroups.com
Btw I think it has to do with the package-info.java file that is not
properly handled by the compiler used in play 1.0.x. I've updated it
in play 1.1

The Dave

unread,
Sep 2, 2010, 12:15:46 PM9/2/10
to play-framework
We're using JAXB and haven't encountered the same problems. When we
produce the JAXBContext, we use this method:

JAXBContext context = JAXBContext.newInstance(XmlAnnotatedType.class);

and here's how we unmarshall:

JAXBElement<XmlAnnotatedType> processElement =
(JAXBElement<XmlAnnotatedType>) unmarshaller.unmarshal(messageInput);
XmlAnnotatedType processMessage = processElement.getValue();

If I get a second, I'll donwload you example and see if this works on
it.

--Dave

On Sep 2, 6:31 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Btw I think it has to do with the package-info.java file that is not
> properly handled by the compiler used in play 1.0.x. I've updated it
> in play 1.1
>
> On Thu, Sep 2, 2010 at 11:57 AM, Guillaume Bort
>
>
>
> <guillaume.b...@gmail.com> wrote:
> > Yes same here. Don't know why but it works with play 1.1-beta1
>
> > On Thu, Sep 2, 2010 at 10:52 AM, Julien Tournay <boudhe...@gmail.com> wrote:
> >> Opps sorry for double posting, iPhone problem....
> >> I've tested it, it fails with play 1.0.x, but works with 1.1.
> >> Can you use 1.1 ?
> >> Julien.
>
> >> On Thu, Sep 2, 2010 at 9:30 AM, Julien Tournay <boudhe...@gmail.com> wrote:
>
> >>> Do you use annotations or a xsd to configure binding?
>
> >>> On Wednesday, September 1, 2010, John Goodsen <jgood...@radsoft.com>
> > Guillaume Bort,http://guillaume.bort.fr
>
> > For anything work-related, use g...@zenexity.fr; for everything else,
> > write guillaume.b...@gmail.com
>
> --
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

The Dave

unread,
Sep 2, 2010, 1:11:04 PM9/2/10
to play-framework
I downloaded your example, and here's a modification that works. I'm
not sure if this will solve your problem sufficiently, but it's
hopefully a step in the right direction.

public static void jaxb_parse_test() throws Exception {

String data = readFileAsString("test/states.xml");
assertTrue("sanity check to make sure we're reading the sample
XML file", data.contains("ArrayOfSetting"));

ByteArrayInputStream source = new
ByteArrayInputStream(data.getBytes());
source.reset(); // necessary?

JAXBContext jc = JAXBContext.newInstance("com.test");
Unmarshaller u = jc.createUnmarshaller();
JAXBElement<ArrayOfSetting> obj = null;
obj = (JAXBElement<ArrayOfSetting>) u.unmarshal(new
StreamSource(source), ArrayOfSetting.class);

ArrayOfSetting val = obj.getValue();
assertTrue(val.toString().startsWith("ArrayOfSetting"));

renderText("It works!");

John Goodsen

unread,
Sep 2, 2010, 2:45:59 PM9/2/10
to play-framework
doh! I will never be afraid to try a beta again. I will never be
afraid to try a beta again. I will never be afraid to try a beta
again ... :-)

thanks so much. Play framework rocks and that's coming from a 5 yr
Ruby on Rails veteran.

jg
Reply all
Reply to author
Forward
0 new messages