java 具有 JAXB 随机 ClassCastException 的 Netbeans .. 无法转换为 com.sun.xml.bind.v2.runtime.reflect.Accessor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15569395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Netbeans with JAXB Random ClassCastException ..cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
提问by Farouk Alhassan
I have downloaded the Soap messages from a SOAP Service and trying to mock the Soap service by returning the downloaded messages. the following code shows how I am Unmarshalling the Soap message into the required Response
我已经从 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();
}
However I keep getting this ClassCastExeption which happens randomly. After a number of test iterations, it begins to happen. Sometimes a clean and build fixes it but sometimes it doesn't work.
但是,我不断收到这个随机发生的 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)
I have tried other online suggestions such as reverting to old jaxb versions and using endorsed folders in the maven compiler configuration but it still happens
我尝试了其他在线建议,例如恢复到旧的 jaxb 版本并在 Maven 编译器配置中使用认可的文件夹,但它仍然发生
Any ideas on what could be causing it and possible solutions?
关于可能导致它的原因和可能的解决方案的任何想法?
Thank u
感谢你
回答by Farouk Alhassan
Solved with the following code
用以下代码解决
@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");
}
Parameter can also be set at JVM using
参数也可以在 JVM 中使用
-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true
回答by Ritzbot
I encountered the same error when I tried to upgrade JAXB to a newer version than what came with the JDK. Java encountered two or more instances of JAXB at runtime and could not decide which version to use.
当我尝试将 JAXB 升级到比 JDK 附带的版本更新的版本时,我遇到了同样的错误。Java 在运行时遇到两个或多个 JAXB 实例,无法决定使用哪个版本。
In my case, the problem was that my application used web services, and I hadn't externalized JAX-WS as well. The application started out using com.sun.xml.bind.v2.runtime classes, but when it began working with a WSDL file, the internal JAX-WS tried to invoke com.sun.xml.internal.bind.v2.runtime classes. The error went away when I downloaded and installed JAX-WS, and I was able to continue the upgrade.
就我而言,问题在于我的应用程序使用了 Web 服务,而且我还没有外部化 JAX-WS。该应用程序开始使用 com.sun.xml.bind.v2.runtime 类,但是当它开始使用 WSDL 文件时,内部 JAX-WS 尝试调用 com.sun.xml。内部.bind.v2.runtime 类。当我下载并安装 JAX-WS 时,错误消失了,我能够继续升级。
回答by DDRider62
I ran into the same issue in one of my applications. In my case, the project was using a library compiled with Java 1.5 compatibility while the main project had compatibility with version 1.6. When I changed both to use 1.6 the problem went away. I hope this will be able to help someone since the problem can be quite frustrating and hard to track.
我在我的一个应用程序中遇到了同样的问题。就我而言,该项目使用的是兼容 Java 1.5 的库,而主项目兼容 1.6 版。当我将两者都改为使用 1.6 时,问题就消失了。我希望这能够帮助某人,因为问题可能非常令人沮丧且难以追踪。
回答by L42
The accepted solution worked for me when in Intellij, but I got the same error when running maven from the command line. Adding this to the configuration for maven-surefire-plugin
solved the issue there as well:
在 Intellij 中接受的解决方案对我有用,但是从命令行运行 maven 时我遇到了同样的错误。将此添加到配置中以maven-surefire-plugin
解决该问题:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize>true</com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize>
</systemPropertyVariables>
</configuration>
</plugin>
回答by sam22
I removed the dependency on separate jaxb-impl jar from the build.sbt. That works now.
我从 build.sbt 中删除了对单独 jaxb-impl jar 的依赖。这现在有效。
回答by Markus Schulte
I had a similar issue, but I didn't / don't have a dependeny(-version-mismatch)-problem.
我有一个类似的问题,但我没有/没有依赖(-version-mismatch)问题。
In my case, a class was missing a @XmlAccessorType, adding this annotation solved my problem. Adapted to your case, the solution would be:
就我而言,一个类缺少@XmlAccessorType,添加此注释解决了我的问题。根据您的情况,解决方案是:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD) // check, if @XmlAccessorType is missing
public class GetClientByAccountResponse {
..
回答by Baskar23
Including jaxbiimpl jar during run time through your dependency manager in parent pom will resolve this issue. This solution is specific to maven projects.
在运行时通过父 pom 中的依赖项管理器包含 jaxbiimpl jar 将解决此问题。此解决方案特定于 Maven 项目。