JAXB classes from Webservice sort error

I have several JAXB classes created by wsimport

wsimport -d src/main/java -keep -extension -p my.package http://www.OpenLigaDB.de/Webservices/Sportsdata.asmx?WSDL 

I will demonstrate a problem with this class (only one @XmlRootElement element was added by me):

 package my.package; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Sport", propOrder = {"sportsID","sportsName"}) @XmlRootElement(name = "sport") //Added by myself public class Sport { protected int sportsID; protected String sportsName; public int getSportsID() {return sportsID;} public void setSportsID(int value) {this.sportsID = value;} public String getSportsName() {return sportsName;} public void setSportsName(String value) {this.sportsName = value;} } 

Direcly instantiation and sorting work fine ( Example1 )

 Sport sport = new Sport(); sport.setSportsID(1); sport.setSportsName("test"); JAXBContext jc = JAXBContext.newInstance(Sport.class); jc.createMarshaller().marshal(sport,System.out); 

Now let's create an object inside the webservice call:

 SportsdataSoap s = new Sportsdata().getSportsdataSoap(); ArrayOfSport sports = s.getAvailSports(); for(Sport sport : sports.getSport()) { JAXBContext jc = JAXBContext.newInstance(Sport.class); jc.createMarshaller().marshal(sport,System.out); } 

Then I got this exception:

 Exception in thread "main" java.lang.ClassCastException: my.package.Sport$JaxbAccessorF_sportsID 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:199) at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:191) at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:282) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:94) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:128) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:183) at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:526) at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:341) at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1158) at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202) at javax.xml.bind.ContextFinder.find(ContextFinder.java:363) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522) at my.package.TestOpenLiga.testDisciplines(TestOpenLiga.java:48) at my.package.TestOpenLiga.main(TestOpenLiga.java:130) 

How to deal with this? Thanks, Thor.

Update 2 If I change Example1 , I get the same error

 SportsdataSoap s = new Sportsdata().getSportsdataSoap(); Sport sport = new Sport(); sport.setSportsID(1); sport.setSportsName("test"); JAXBContext jc = JAXBContext.newInstance(Sport.class); jc.createMarshaller().marshal(sport,System.out); 

Update 2 XML Sports Structure

 <Sport> <sportsID>int</sportsID> <sportsName>string</sportsName> </Sport> 
+7
source share
4 answers

I am not sure that this is the only reason why this can happen, but this is one of the problems that I encountered and solved this way: check your certified Java directory and delete any duplicate jaxb-impl.jar. This can even happen with webservice-api.jar. Hope this helps.

+2
source

I know this question is old, but I had a similar problem, and I would like to offer my solution to the problem here.

The problem was that we had our own jaxbcontext instance in the same classloader where the jws JAXB context was located.

So we added class A to our JAXB context, and jws added class A to its JAXB context.

When using jws code, the class loader provided the wrong class, and a class exception was thrown.

Using JAXB. (un) marshal functions for this "common" object, we could solve the problem.

UPDATE: Well, since two are asking, I will update this question after 4 years :-)

The problem described above indicates a hierarchical classloader problem. JAXB generates a class (un) of sorting objects on the fly from annotations and adds them to its cache. At first, these classes seem to be the same, but they represent different class objects for the same annotations. This works because they are not in the same classloader, and the server-side classloader will not plunge the class tree into another classloader to find them.

When we mixed objects from the lower class loader with the class loader above the hierarchy, marshalling could not match.

This is how I remember the problem.

+2
source

I recently received an error message when I tried to upgrade JAXB to a newer version than what was in the JDK. I had an external version of JAXB, but my JAX-WS was still the internal version that came with the JDK. Internal JAX-WS tried to call internal JAXB, but the application wanted to use external JAXB.

In fact, both JAXB and JAX-WS must be internal or both must be external. This thread has advice if you have external JAXB and want to switch to using the internal version.

+1
source

I also cross-check this error from time to time (it was not constant, disappointed !!), and I resolved it with this answer : I add httpClientPolicy.setAllowChunking(false); into your code and \ o / is no longer a problem :)

But really, I don’t know why: /

-2
source

All Articles