ClassCastException: cannot be added to com.sun.xml.internal.bind.v2.runtime.reflect.Accessor

I have a SOAP web service that I am trying to call inside an application. I use cxf-codegen-plugin (3.1.10) to generate sources from WSDL.

Using the generated client, if I call the web service in the application, it works fine. However, I also use a different instance of JAXB against the same package in the application that causes the problem.

For example, the following works fine:

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

However, initializing a new JAXB instance right before calling getOutboundServicePort() to fail:

 JAXBContext.newInstance(SendMessageRequest.class); OutboundServicePortType service = new OutboundService().getOutboundServicePort(); service.sendMessage(message); 

Using the following stacktrace command:

 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 

Things I've tried so far:

JAXB classes from Marshalling Webservice error

  • I do not have approved jars in the JDK folder, so this answer does not apply
  • Jaxb-impl jar (2.2.11) comes from camel-jaxb in my application, so it seems to be very inclusive and not like this answer .
  • This answer seems to describe the problem well, but the solution they made seems confusing to me.

Problems creating JAXBContext to marshal an object as XML

  • This question seems identical to me, but the solution they ended up with cannot work for my situation (see below).

Netbeans with JAXB Random exception ClassCastException .. will not be passed to com.sun.xml.bind.v2.runtime.reflect.Accessor

  • I tried the solution System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true"); that works. 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 doesn't address the real problem (if I don't understand it).

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

+12
source share
4 answers

I hate answering my question, but I wanted to make sure that the solution with which I finished work was clearly fixed.

The root problem was that the jaxb-impl jar, provided by camel-jaxb, was inconsistent with the version provided by JDK 8.

This answer describes what happens more clearly:

I encountered the same error when trying to upgrade JAXB to a newer version than what was in the JDK. Java encountered two or more JAXB instances at runtime and could not decide which version to use.

In my case, I just eliminated the jaxb-impl that came with camel-jaxb and the application started working correctly.

+10
source

I am facing the same problem in testing. For me, adding jaxws-rt runtime jar helped. Please note that I added it only for my test area. You are not going to do this for the prod runtime, as it is assumed that this or another implementation is already in the JEE container.

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

I encountered this error while trying to run my JUnit tests through Maven, even if they passed locally in Eclipse. My solution was to put in @BeforeClass:

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

0
source

I do not have enough reputation to comment, but I want to emphasize that an early simple fix is ​​cool. Exclude "jaxb-impl that came with camel-jaxb". My mistake was

 java.lang.ClassCastException: ...defbase.DataResponse$JaxbAccessorM_getMessages_setMessages_java_util_List cannot be cast to com.sun.xml.internal.bind.v2.runtime.reflect.Accessor 
0
source

Source: https://habr.com/ru/post/1015325/


All Articles