1.下载axis,解压后把webapps中的axis文件夹copy到web服务器中的webappa下,这样就可以浏览了。
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface ( DII)
2、Stubs方式
3、Dynamic Proxy方式
2.http://localhost/axis/services 可以查看当前的服务。
3.第一个小程序:( DII)
服务器端:(保存为Test.jws在上面提到的axis文件夹下)
public class Test{
//fields
private String name="gaga";
private int age=20;
private String message;
private List items=new ArrayList();
//method at here.
public String getName(){
return name;
}
public int getAge(){
return age;
}
public List getItems(){
return items;
}
public void setMessage(String message){
this.message=message;
System.out.println(message);
}
}
访问连接http://localhost/axis/Test.jws?wsdl :页面显示Axis自动生成的wsdl
client端程序:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class TestWebService {
public static void main(String args[]) {
System.out.println("Start invoking.");
try {
String endpoint = "http://localhost/axis/Test.jws";// 你写的那个文件
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("getAge");// 填写你要调用的方法名称
int ret = Integer.parseInt(("" + call.invoke(new Object[] {})));
System.out.println(ret);
call.setOperationName("setMessage"); //代参数的调用
// new Object[]{"test","test2"}传递多个参数
Object [] arguments=new Object [1];
arguments[0]="hello";
call.invoke(arguments);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
运行结果:
Start invoking....
20
Finished the invoking.
同时:服务器端输出hello
实例2(Dynamic Proxy)
1.在axis/src下新建MyServiceInterface.java,MyService
import java.rmi.RemoteException;
public interface MyServiceInterface extends Remote {
public String processService(String arg) throws RemoteException;
}
public String processService(String arg){
return arg; }
}
然后将MyService copy到axis下保存为MyService.jws
2.client端代码如下:
String wsdlUrl = "http://localhost:8081/axis_example/jws/MyService.jws?wsdl";
String nameSpaceUri = "http://localhost:8081/axis_example/jws/MyService.jws";
String serviceName = "MyServiceService";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
javax.xml.rpc.Service service = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName));
MyServiceInterface proxy = (MyServiceInterface)
service.getPort(new QName(nameSpaceUri, portName), MyServiceInterface.class);
System.out.println("This is " + proxy.processService("Dynamic Proxy test!"));
}
实例3(stubs):
1.在工程文件夹下建立Myservice:
public String processService(String arg){
return arg; }
}
2.新建deploy.wsdd(参考axis-bin-1_4.zip \axis-1_4\samples\deploy.wsdd)
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MyService" provider="java:RPC">
<parameter name="className" value="MyService"/>
<parameter name="allowedMethods" value="processService"/>
</service>
</deployment>
3.启动服务器,执行
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd
执行后可以看到在web-inf下生成server-config.wsdd
4。重启服务。
5。更改client.
// 指出service所在URL
String endpoint = "http://localhost:" + "8081" + "/axis_example/services/MyService";
// 创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// 方法名(processService)与MyService.java方法名保持一致
call.setOperationName("processService");
// Object 数组封装了参数,参数为"This is Test!",调用processService(String arg)
String ret = (String) call.invoke(new Object[]{"This is Test!"});
System.out.println(ret);
}
注: 在这里可以看出, DII 方式安全性不高(url MyService.jws为axis自动生成),且无法进行一些复杂的配置, Dynamic Invocation Interface(DII) 和 Stubs 方式的区别主要有两个地方:
① 两种不同的 endpoint
DII :http://localhost:8081/axis_example/jws/MyService.jws
Stubs :http://localhost:8081/axis_example/services/MyService
② 两种不同的编译方式
DII :根据endpoint访问web service时,axis自动编译endpoint指定的*.jws文件,并放在生成的WEB-INF/jwsClasses目录下。
Stubs :手工编译java文件,手工编写server-config.wsdd配置文件(这里可以编写deploy.wsdd,用axis提供的java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd 命令生成server-config.wsdd文件中的其他通用部分)
而Dynamic Proxy方式仅仅在DII的基础上采用了代理机制,实际上和DII区别不大。