JAXB Unmarshall Created an Empty Object
Asked Answered
F

5

21

I am using JAXB to unmarshal an XML file into an object tree. The root object is non-null, but all of it's members are null even though there is data in the XML file. My object classes were generated with Axis2 from wsdls. I have the ObjectFactory class, the jaxb.index class, the package.info annotation, etc.

My problem is the same as this discussion: http://old.nabble.com/AXIS2,-JAXB---Unmarshalling-td26847419.html

First and foremost: is there a way I can get it to fill the data properly?

If not, is there a good substitute library for either Axis2 or JAXB that does the same thing but that will play nicely with each other?

Fluttery answered 13/4, 2010 at 21:16 Comment(2)
We'll need some sample JAXB classes and XML before we can answer this.Vite
Did you figure out what caused this problem?Abra
V
59

JAXB by default silently ignores errors. I cannot see any reason why this is the default configuration. Try adding this code to throw an exception if something goes wrong.

unmarshaller.setEventHandler(
    new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent event ) {
            throw new RuntimeException(event.getMessage(),
                                       event.getLinkedException());
        }
});
Volz answered 6/7, 2010 at 22:6 Comment(5)
Great suggestion. I would however caution against considering the JAXB developers as not very good - much of JAXB was written by the genius behind Jenkins, Kohsuke Kawaguchi. Allowing for a pluggable event handler allows for great flexibility (at a cost of simplicity).Kaine
@Kaine - You're right, I shouldn't use those words. However, having just discovered that I'd wasted two days of work because JAXB was silently ignoring errors was frustrating, to say the least. I strongly maintain that this should not be the default configuration, but I will edit the wording on my answer.Volz
If you use Jaxb2Marshaller, it's unmarshaller.setValidationEventHandler().Landsman
I was wasting a lot of time trying to find whats wrong with my pojos. This finally was the biggest help :)Whirlybird
Another alternative is to use the DefaultValidationHandler: unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());Adhesion
A
1

Recently I had similar kind of issue, and could able to fix it up as below:

1) Fixup the xsd file. In my case I've ensured that all complex elements in xsd refering to their corresponding type using ref attribute, instead of declaring them with type attribute.

I've verified whether my xsd proper or not by creating an xml file from XML Schema file option in Eclipse.

Before the fix, the root element in the xml file was empty. After fixing the xsd the xml was got created properly with some sample values.

2) clean and re-build It's mandatory to clean before re-generating the classes.

Ade answered 8/6, 2011 at 10:16 Comment(0)
M
0

Just want to add that when Axis2 is used together with Rampart (SOAP Body encryption) and JAXB, this issue occurs always.

See for details: AXIS2-4981

Margarettmargaretta answered 29/3, 2011 at 12:22 Comment(0)
K
0

The package-info.java file must be compiled. Eclipse must find all .java files and compile them. If compiling from the command line, and letting javac search for dependencies, it will NOT find it. Then when you run you'll get empty objects. Seems like a bug in JAXB to not at least import that file somewhere. And the error should tell you to possible compile that file.

Kelsi answered 19/6, 2012 at 17:56 Comment(0)
K
0

I came across this issue when the xsd files that were used to create the Java code had references to unreachable urls.

Luckily all of these references appeared in the package-info.java files so I eventually had to add an antRun task after the xjc task that deleted them.

Kinsfolk answered 10/7, 2019 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.