我有一些wsimport生成的JAXB类
wsimport -d src/main/java -keep -extension
-p my.package
http://www.OpenLigaDB.de/Webservices/Sportsdata.asmx?WSDL
我将演示此类的问题(我自己仅添加了@XmlRootElement):
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;}
}
直接实例化和编组工作正常( Example1 )
Sport sport = new Sport();
sport.setSportsID(1);
sport.setSportsName("test");
JAXBContext jc = JAXBContext.newInstance(Sport.class);
jc.createMarshaller().marshal(sport,System.out);
现在让我们在网络服务调用中创建对象:
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);
}
然后我得到了这个异常:
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)
怎么处理呢?谢谢你,雷神
更新2 如果我将实例1 修改为此,则会收到相同的错误
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);
更新2 运动的XML结构
<Sport>
<sportsID>int</sportsID>
<sportsName>string</sportsName>
</Sport>
最佳答案
我不确定这是否是发生这种情况的唯一原因,但这是我面临和解决的一个问题,请检查Java认可的目录并删除所有重复的jaxb-impl.jar。即使使用webservice-api.jar也可能发生这种情况。希望这可以帮助。