Far*_*san 21 java netbeans jaxb jaxb2 netbeans-7
我从SOAP服务下载了Soap消息,并尝试通过返回下载的消息来模拟Soap服务.以下代码显示了我如何将Soap消息解组为所需的响应
public static DataClientType unmarshallFile(String fileName) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(ClientSampleSoapResponseData.class.getResourceAsStream(fileName));
xsr.nextTag(); // Advance to Envelope tag
xsr.nextTag(); // Advance to Header
xsr.nextTag(); // Advance to Body tag
xsr.nextTag(); // Advance to getClientByAccountResponse
xsr.nextTag(); // Advance to content of getClientByAccountResponse
JAXBContext jc = JAXBContext.newInstance(GetClientByAccountResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<GetClientByAccountResponse> je = unmarshaller.unmarshal(xsr, GetClientByAccountResponse.class);
return je.getValue().getClientDataContract();
}
Run Code Online (Sandbox Code Playgroud)
但是我继续得到随机发生的ClassCastExeption.经过多次测试迭代后,它开始发生.有时一个干净的构建修复它,但有时它不起作用.
java.lang.ClassCastException: com.x.X.X.X.GetClientByAccountResponse$JaxbAccessorF_clientDataContract cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:188)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:180)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:256)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过其他在线建议,例如恢复旧的jaxb版本并在maven编译器配置中使用已签名的文件夹,但它仍然会发生
关于什么可能导致它和可能的解决方案的任何想法?
感谢你
Far*_*san 34
解决了以下代码
@BeforeClass
public static void init(){
System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");
}
@AfterClass
public static void revert(){
System.getProperties().remove("com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize");
}
Run Code Online (Sandbox Code Playgroud)
也可以使用JVM设置参数
-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true
Run Code Online (Sandbox Code Playgroud)
小智 5
当我尝试将JAXB升级到比JDK附带的版本更新的版本时,我遇到了同样的错误.Java在运行时遇到两个或多个JAXB实例,无法确定要使用哪个版本.
在我的例子中,问题是我的应用程序使用了Web服务,我也没有外化JAX-WS.该应用程序使用com.sun.xml.bind.v2.runtime类开始,但当它开始使用WSDL文件时,内部JAX-WS尝试调用com.sun.xml.内部 .bind.v2.runtime类.当我下载并安装JAX-WS时,错误消失了,我能够继续升级.
| 归档时间: |
|
| 查看次数: |
12700 次 |
| 最近记录: |