2

I need to run some validation code after my JAXB object is unmarshalled. My unmarshaller's event handler is set to the DefaultValidationEventHandler.

Before I added an afterUnmarshal() method, when my code threw an Exception, it came out like this:

DefaultValidationEventHandler: [ERROR]: com.example.ExceptionClass Error message
     Location: line 3

Now, however, I'm getting

DefaultValidationEventHandler: [ERROR]: null
     Location: line 3

My afterUnmarshal()

void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
    //validation code that may throw a runtime exception
}

Is there something I'm supposed to put into my afterUnmarshal() method to keep the exception details? What am I missing?

4

1 回答 1

2

我无法重现您在问题中描述的内容。以下是我尝试过的。

JAVA模型

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    private Bar bar;

}

酒吧

该类Bar只包含一个afterUnmarshal抛出RuntimeException.

import javax.xml.bind.Unmarshaller;

public class Bar {

    void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
        throw new RuntimeException("Hello World");
    }

}

演示代码

在下面的演示代码中,我们将解组一个将触发类afterUnmarshal上的方法的文档Bar

import java.io.StringReader;

import javax.xml.bind.*;
import javax.xml.bind.helpers.DefaultValidationEventHandler;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setEventHandler(new DefaultValidationEventHandler());

        StringReader xml = new StringReader("<foo><bar/></foo>");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);
    }

}

输出

我们抛出的RuntimeException显示在堆栈跟踪中。

Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[java.lang.reflect.InvocationTargetException]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleError(UnmarshallingContext.java:671)
    at com.sun.xml.bind.v2.runtime.JaxBeanInfo.invokeUnmarshallCallback(JaxBeanInfo.java:555)
    at com.sun.xml.bind.v2.runtime.JaxBeanInfo.invokeAfterUnmarshalMethod(JaxBeanInfo.java:546)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.fireAfterUnmarshal(Loader.java:225)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.leaveElement(StructureLoader.java:271)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:506)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:156)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:183)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:353)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
    at forum15142943.Demo.main(Demo.java:17)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.xml.bind.v2.runtime.JaxBeanInfo.invokeUnmarshallCallback(JaxBeanInfo.java:551)
    ... 22 more
Caused by: java.lang.RuntimeException: Hello World
    at forum15142943.Bar.afterUnmarshal(Bar.java:8)
    ... 27 more
于 2013-03-01T01:11:24.653 回答