java ClassCastException:无法转换为 com.sun.xml.internal.bind.v2.runtime.reflect.Accessor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42499436/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:41:28  来源:igfitidea点击:

ClassCastException: Cannot be cast to com.sun.xml.internal.bind.v2.runtime.reflect.Accessor

javajaxbcxf

提问by Tyler Murry

I have a SOAP webservice that I am trying to call inside of an application. I am using the cxf-codegen-plugin (3.1.10) to generate sources from the WSDL.

我有一个 SOAP 网络服务,我试图在应用程序内部调用它。我正在使用 cxf-codegen-plugin (3.1.10) 从 WSDL 生成源。

Using the generated client, if I call the webservice within the application, it works great. However, I am also using another JAXB instance against the same package within the application which is causing an issue.

使用生成的客户端,如果我在应用程序中调用 web 服务,效果很好。但是,我还针对应用程序中的同一个包使用了另一个 JAXB 实例,这导致了问题。

For example, the following works great:

例如,以下效果很好:

OutboundServicePortType service = new OutboundService().getOutboundServicePort();
service.sendMessage(message);

However, initializing a new JAXB instance right before causes the getOutboundServicePort()call to fail:

但是,在之前初始化一个新的 JAXB 实例会导致getOutboundServicePort()调用失败:

JAXBContext.newInstance(SendMessageRequest.class);

OutboundServicePortType service = new OutboundService().getOutboundServicePort();
service.sendMessage(message);

With the following stacktrace:

使用以下堆栈跟踪:

Caused by: java.lang.ClassCastException: outbound.model.standard.StandardOutboundMessage$JaxbAccessorF_messageUUId cannot be cast to com.sun.xml.internal.bind.v2.runtime.reflect.Accessor
    at com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:190)
    at com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:179)
    at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:271)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:77)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:153)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:152)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:96)
    at com.sun.xml.internal.ws.developer.JAXBContextFactory.createJAXBContext(JAXBContextFactory.java:98)
    at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:79)
    ... 25 more

Things I have tried so far:

到目前为止我尝试过的事情:

JAXB classes from Webservice marshalling error

来自 Web 服务编组错误的 JAXB 类

  • I have no endorsed jars in my JDK folder, so this answerdoes not apply
  • The jaxb-impl jar (2.2.11) is coming from camel-jaxb in my app so it seems very contained and not like this answersuggests.
  • This answerseems to describe the problem well, but the solution they took seems unclear to me.
  • 我的 JDK 文件夹中没有认可的 jars,所以这个答案不适用
  • jaxb-impl jar(2.2.11)来自我的应用程序中的camel-jaxb,所以它似乎非常包含而不像这个答案所暗示的那样。
  • 这个答案似乎很好地描述了问题,但他们采取的解决方案对我来说似乎不清楚。

Problems creating JAXBContext to marshal object as XML

创建 JAXBContext 以将对象编组为 XML 的问题

  • This question seems to be identical to mine, but the solution they ended up going with can not work for my situation (see below)
  • 这个问题似乎与我的相同,但他们最终采用的解决方案不适用于我的情况(见下文)

Netbeans with JAXB Random ClassCastException ..cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor

具有 JAXB 随机 ClassCastException 的 Netbeans .. 无法转换为 com.sun.xml.bind.v2.runtime.reflect.Accessor

  • I tried the accepted solution System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");which doeswork. However, setting this property is not an option for me unfortunately in my environment. Plus, it seems like it's a bit of a hack that does not address the real issue (unless I'm misunderstanding it).
  • 我试着接受的解决方案System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");,其的工作。但是,不幸的是,在我的环境中,设置此属性不是我的选择。另外,这似乎是一个没有解决真正问题的黑客(除非我误解了它)。

I'm about to hang myself with the little rope I have left. What am I missing here?

我准备用剩下的小绳子吊死自己。我在这里错过了什么?

采纳答案by Tyler Murry

I hate to answer my own question, but I wanted to make sure the solution I ended up going with was documented clearly.

我不想回答我自己的问题,但我想确保我最终采用的解决方案被清楚地记录下来。

The root issue was that the jaxb-impl jar brought in by camel-jaxb was conflicting with the JDK 8 provided version.

根本问题是camel-jaxb 引入的jaxb-impl jar 与JDK 8 提供的版本冲突。

This answerdescribes what is happening more clearly:

这个答案更清楚地描述了正在发生的事情:

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, I simply excluded the jaxb-impl that came with camel-jaxb and the application started working correctly.

就我而言,我只是排除了camel-jaxb 附带的jaxb-impl,应用程序开始正常工作。

回答by Student

I ran into this error when trying to run my JUnit tests through Maven even though they passed locally in Eclipse. My solution was to in a @BeforeClassput:

我在尝试通过 Maven 运行我的 JUnit 测试时遇到了这个错误,即使它们在 Eclipse 中本地通过。我的解决方案是@BeforeClass放入:

System.setProperty("javax.xml.bind.JAXBContext", "com.sun.xml.internal.bind.v2.ContextFactory");

回答by PetroCliff

I run into the same issue in test scope. For me adding jaxws-rt runtime jar helped. Note, that I added it for my test scope only. You are not going to do this for prod runtime, since JEE container supposed to has this or other implementation already.

我在测试范围内遇到了同样的问题。对我来说,添加 jaxws-rt 运行时 jar 有帮助。请注意,我仅为我的测试范围添加了它。您不会为 prod 运行时执行此操作,因为 JEE 容器应该已经具有此或其他实现。

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.2.3</version>
    <scope>test</scope>
</dependency>