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

我有一个SOAP Web服务,我试图在应用程序内部调用.我使用cxf-codegen-plugin(3.1.10)从WSDL生成源代码.

使用生成的客户端,如果我在应用程序中调用Web服务,它可以很好地工作.但是,我还在应用程序中使用另一个JAXB实例来导致问题.

例如,以下工作很好:

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

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

JAXBContext.newInstance(SendMessageRequest.class);

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

使用以下堆栈跟踪:

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$1.createJAXBContext(JAXBContextFactory.java:98)
    at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:79)
    ... 25 more

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

JAXB classes from Webservice marshalling error

>我的JDK文件夹中没有认可的jar,因此this answer不适用
> jaxb-impl jar(2.2.11)来自我的应用程序中的camel-jaxb,因此它似乎非常包含,而不像this answer建议的那样.
> This answer似乎很好地描述了这个问题,但他们采取的解决方案对我来说似乎并不清楚.

Problems creating JAXBContext to marshal object as XML

>这个问题似乎与我的问题相同,但他们最终解决的问题对我的情况不起作用(见下文)

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

>我尝试了接受的解决方案System.setProperty(“com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize”,“true”);哪个确实有效.但是,在我的环境中,不幸的是,设置此属性不是我的选择.此外,它似乎是一个没有解决真正问题的黑客攻击(除非我误解它).

我准备用我留下的小绳子挂上自己.我在这里错过了什么?

解决方法:

我讨厌回答我自己的问题,但我想确保我最终得到的解决方案清楚地记录下来.

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

This answer更清楚地描述了发生的事情:

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.

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

上一篇:win10+Anaconda3+CUDA9.0+CUDNN7.1+TensorFlow-gpu1.9+Pycharm


下一篇:C#调webservice超时;结果:C#调用webservice服务超时