axis发布webservice有很多中方式,基于wsdl,wsdd等等,配置xml太麻烦,还是写点最最基础的:
1. 写一个 java 类有一些基本函数
public class Test{
//fields
private String name="gaga";
public String setName(String name){
this.name=name;
return "hello "+this.name;
}
}
2. axis 的完整包中有 webapps 文件夹,里面有一个 axis 文件夹,把它复制到 $TOMCAT_HOME/webapps/ 下,并将刚才写的 Test.java 改名为 Test.jws(java web service) 并放到 $TOMCAT_HOME/webapps/axis/ 下,启动 tomcat ,可以输入网址: http://localhost:8080/Test.jws?wsdl 查看他的 wsdl 文件,这是由 /webapps/axis 内的文件动态生成的。这个时候其实已经发布 web 服务了。
3. 编写 web service 客户端:(把 axis 的 jar 包加到项目 classpath 中)
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestWebService{
public static void main(String args[]){
System.out.println("Start invoking....");
try {
String endpoint =
"http://localhost:8080/axis/Test.jws";//你写的那个文件
String newName=new String("tianwei");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName("setName");//填写你要调用的方法名称
//call.addParameter("name", XMLType.SOAP_STRING, ParameterMode.IN);
//call.setReturnClass(String.class);
//下面是来调用webservice函数的,Call已经绑定函数setName(String name)
//了,new Object[]{newName}中newName是传入的参数。当然,大括号内可以有
//不止一个参数
String name=(String)call.invoke(new Object[]{newName});
System.out.println(name);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
运行结果 :
Start invoking....
hello tianwei
Finished the invoking.