Apache Axis2 throws NoSuchMethodError when trying

2019-09-04 14:58发布

问题:

I am trying to hit the following web service with axis2: http://www.webservicex.net/geoipservice.asmx?WSDL

I have Axis2 locally, and I generated my classes by downloading the wsdl to my local drive and running:

./wsdl2java.sh -uri geoipservice.wsdl -p geoip -d xmlbeans -s -o geoip

This produced a build directory with a build.xml file in it, which i then ran "ant" in, and it produced a client jar in the libs folder.

I dropped this jar onto my classpath, and then I tried to hit the service using the following code:

GeoIPServiceStub stub = new GeoIPServiceStub("http://www.webservicex.net/geoipservice.asmx?WSDL");
GetGeoIPDocument req = GetGeoIPDocument.Factory.newInstance();
GetGeoIP gic = req.addNewGetGeoIP();
gic.setIPAddress("74.125.91.105");  // google.com
GetGeoIPResponseDocument resp = stub.getGeoIP(req);
System.out.println(resp.getGetGeoIPResponse().toString());

Things seem to run at first, and I see the results coming back in the syslog, but before it finishes it throws this exception:

java.lang.NoSuchMethodError: org.apache.axiom.om.impl.OMStAXWrapper.<init>(Lorg/apache/axiom/om/OMXMLParserWrapper;Lorg/apache/axiom/om/OMElement;Z)V
at org.apache.axiom.om.impl.llom.OMStAXWrapper.<init>(OMStAXWrapper.java:52)
at org.apache.axiom.om.impl.llom.OMElementImpl.getXMLStreamReader(OMElementImpl.java:795)
at org.apache.axiom.om.impl.llom.OMElementImpl.getXMLStreamReaderWithoutCaching(OMElementImpl.java:765)
at geoip.GeoIPServiceStub.fromOM(GeoIPServiceStub.java:767)
at geoip.GeoIPServiceStub.getGeoIP(GeoIPServiceStub.java:325)

I've googled all over and haven't been able to find what is causing this. Can anyone help me find what I'm doing wrong? Thank you.

回答1:

You get NoSuchMethodErrors if your compiler used a different class definition (during compilation) to create your .class file then the jvm has at runtime.

In your case, it misses the constructor for org.apache.axiom.om.impl.OMStAXWrapper(org.apache.axiom.om.OMXMLParserWrapper, org.apache.axiom.om.OMElement)

It probably means you have the wrong org.apache.axiom library somewhere on your classpath.