参照网上的例子写了一个AXIS2的客户端例子,在此感谢各位前辈的无私奉献!同时也分享一下,供各位朋友指导!
服务端代码:
import java.util.Random;
public class HelloWorldService {
public String sayHello(String name) {
return name + "! say:hello [axis2 ]";
}
public int getAge(int i) {
return i + new Random().nextInt(100);
}
}
客户端代码:
package client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class HelloWordClient {
public static void main(String[] args) throws AxisFault {
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
String address = "http://localhost:8080/axis2/services/HelloWorldService";
EndpointReference epf = new EndpointReference(address);
options.setTo(epf);
QName qName = new QName("http://ws.apache.org/axis2","sayHello");
Object[] result = client.invokeBlocking(qName, new Object[]{"winter"}, new Class[]{String.class});
System.out.println(result[0]);
qName=new QName("http://ws.apache.org/axis2","getAge");
result = client.invokeBlocking(qName, new Object[]{ new Integer(22) } , new Class[] {int.class});
System.out.println(result[0]);
}
}
以上代码接来源于网络!
下面才是我重点想说的内容,也是我一样的初学者比较困惑的jar包问题,经我实际测试所需的最少jar包为
axiom-api-1.2.13.jar
axiom-impl-1.2.13.jar
axis2-adb-1.6.2.jar
axis2-kernel-1.6.2.jar
axis2-transport-http-1.6.2.jar
axis2-transport-local-1.6.2.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
httpcore-4.0.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
下面是缺少jar包和相关的错误说明:
XmlSchema-1.4.7.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/ws/commons/schema/resolver/URIResolver
axiom-impl-1.2.13.jar
Exception in thread "main" org.apache.axiom.om.OMException: No meta factory found for feature 'default'; this usually means that axiom-impl.jar is not in the classpath
neethi-3.0.2.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/neethi/PolicyComponent
axis2-transport-http-1.6.2.jar
Exception in thread "main" org.apache.axis2.deployment.DeploymentException: org.apache.axis2.transport.http.CommonsHTTPTransportSender
axis2-transport-local-1.6.2.jar
Exception in thread "main" org.apache.axis2.deployment.DeploymentException: org.apache.axis2.transport.local.LocalTransportSender
commons-codec-1.3.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
commons-httpclient-3.1.jar
Exception in thread "main" org.apache.axis2.deployment.DeploymentException: org/apache/commons/httpclient/HttpException
httpcore-4.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpResponseFactory
wsdl4j-1.6.2.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/wsdl/xml/WSDLLocator