编辑#1:我使用了JAXB RI 2.2.6(截至今天12/06/12的最新版本)和我 观察相同的行为: - (
我正在使用JAXB来解组XML有效负载。 当我第一次通过Unmarshaller传递无效的有效负载时,我得到以下类型的错误:
javax.xml.bind.UnmarshalException: Unable to create an instance of foo.bar.FooBarType
- with linked exception:
[java.lang.InstantiationException]
在我的代码的下一行(在catch()块中),我尝试相同的Unmarshaller实例并提供有效的有效负载,我得到以下类型的错误:
java.lang.ClassCastException: foo.bar.SomeAType cannot be cast to foo.bar.SomeBType
在下一行(再次在后续的catch()中),我尝试使用相同的Unmarshaller实例并再次提供相同的有效负载,这样可以解组!
有没有人在JAXB上遇到过这种行为?我在这里遇到一个错误吗?在哪里看/挖?
以下是我正在使用的JAXB jar清单的详细信息:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_12-b04 (Sun Microsystems Inc.)
Specification-Title: Java Architecture for XML Binding
Specification-Version: 2.1
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: JAXB Reference Implementation
Implementation-Version: 2.1.12
Implementation-Vendor: Sun Microsystems, Inc.
Implementation-Vendor-Id: com.sun
Extension-Name: com.sun.xml.bind
Build-Id: hudson-jaxb-ri-2.1-833
Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar
Name: com.sun.xml.bind.v2.runtime
Implementation-Version: hudson-jaxb-ri-2.1-833
提前多多谢谢你!
编辑#2:无法粘贴所有内容,但这是它看起来像的主旨:
final JAXBContext jaxbContext = JAXBContext.newInstance(FooRequestType.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<FooRequestType> result = null;
try {
//try unmarshalling invalid payload.
result = unmarshaller.unmarshal(invalidPayload.getDocumentElement(), FooRequestType.class);
}catch(Exception e1){
try {
//now try unmarshalling valid payload . ##NOTE: If you try unmarshalling Invalid Payload here, it will fail again.
result = unmarshaller.unmarshal(validPayload.getDocumentElement(), FooRequestType.class);
} catch(Exception e2){
//try again unmarshalling valid payload
result = unmarshaller.unmarshal(validPayload.getDocumentElement(), FooRequestType.class);
System.out.println("Result:=" + result.getValue());
System.out.println("Successfully unmarshalled. Exiting with (0)");
System.exit(0);
}
}
System.out.println("Not expecting to reach here.Exiting with (1)");
System.exit(1);
编辑#3:使用EclipseLink MOXy作为JAXB Impl,使用同一段代码;它的行为正确。 我在Sun / Oracle JAXB RI上打开了一个错误:http://java.net/jira/browse/JAXB-931