« Importing private keys into a Java keystore using keytool | Main | "Find It, Fix It, Move On With Your Life" »

JAXB2: Marshalling fails due to validation, but you can't see the error

Having used JAXB2 to construct a tree of XML from objects, you try and marshall your objects to XML, only to find you get an Exception telling you your resulting XML has failed validation. But as you cannot see the generated XML, you have no way to see where you've gone wrong.


JAXB2 has a deprecated the javax.xml.bind.Validator class, and the person who documented this didn't bother to document the mechanism to replace it.

What I did manage to find was the javax.xml.bind.util.ValidationEventCollector class, which you can add to your Marshaller object, like this:

			ValidationEventCollector collector = new ValidationEventCollector();

Now the marshaling works! And your errors can be retrieved from ValidationEventCollector, like this:


Hopefully this will save someone some pain.


TrackBack URL for this entry:

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)