1.下载 axis-bin-1_4.zip 把axis-bin-1_4.zip\axis-1_4\webapps\下的axis项目放入apache中部署。
2.new HelloWorld.java
package com.test;
public class HelloWorld {
public String say(String name){
return "hello:"+name;
}
}
编译成class 放入axis\WEB-INF\classes
3.new deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MyService" provider="java:RPC">
<parameter name="className" value="com.test.HelloWorld"/>
<parameter name="allowedMethods" value="say"/>
</service>
</deployment>
放入axis\WEB-INF\
4.启动tomcat
5.cmd进入命令窗口 进入apache-tomcat-6.0.16\webapps\axis\WEB-INF下执行
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/servlet/AxisServlet deploy.wsdd
执行后可看到在axis_example/WEB-INF目录下生成server-config.wsdd文件
6.重新启动tomcat ,以便加载 server-config.wsdd 文件。
7.写客户端调用测试。
在eclipse中new -》 other -》 web services -》 web Service Client
http://localhost:8080/axis/services/MyService?wsdl
路径中axis是项目名,services是在web.xml中配置,MyService?wsdl是在deploy.wsdd中定义的service name。
public class TestWebservice {
private static HelloWorldServiceLocator localtor = new HelloWorldServiceLocator();
public static void main(String[] args) {
try {
String say = localtor.getMyService().say("hzy");
System.out.println(say);
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
写客户端调用程序
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
String method = "say";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL("http://localhost:8082/axis/services/MyService"));
call.setOperationName(new QName("http://com/", method));
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setSOAPActionURI("http://com/GetServerList");
String k = (String)call.invoke(new Object[]{}); //因为返回值是String类型,所以这里调用的返回值也是String类型
System.out.println(">>> "+k); //返回值输出
}
}