Thursday 14 March 2013

How to make JAXB marshal/unmarshal show errors

JAXB by default silently ignores errors. Try adding this code to throw an exception if something goes wrong.
unmarshaller.setEventHandler(
    new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event ) {
            ValidationEventLocator locator = event.getLocator();
            String errorMessage = "Line:Column[" + locator.getLineNumber() + ":" + locator.getColumnNumber() + "]:" + event.getMessage();
            throw new RuntimeException(errorMessage, event.getLinkedException());
        }
});