解组JAXB对象后,需要运行一些验证代码。我的解组器的事件处理程序设置为DefaultValidationEventHandler

在添加afterUnmarshal()方法之前,当我的代码引发Exception时,它像这样出现:

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


但是,现在

DefaultValidationEventHandler: [ERROR]: null
     Location: line 3


我的afterUnmarshal()

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


我应该在afterUnmarshal()方法中放入一些东西来保留异常详细信息吗?我想念什么?

最佳答案

我无法复制您在问题中描述的内容。下面是我尝试过的。

JAVA模型

oo

import javax.xml.bind.annotation.*;

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

    private Bar bar;

}


酒吧

Bar类仅包含引发afterUnmarshalRuntimeException方法。

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

09-25 20:19