1.环境:
JDK1.5
TOMCAT 5.5.23
MYECLIPSE6.6GA
Axis1.4
2.Axis 支持三种web service开发方式,分别为:
1 、Dynamic Invocation Interface ( DII)
2 、Dynamic Proxy方式
3 、Stubs方式
(本实例只讨论 stubs 方式)
3. 本实例工程axis_example目录结构如下:
-axis_example
----jws
----src
----WEB-INF
---classes
---lib
目录说明如下:
jws :存放*.jws文件
src :java源码
WEB-INF/classes :java编译后的class文件
WEB-INF/lib :需要用到的jar包
4.配置步骤:
1.建立web project 工程,添加AXIS所需要的包。
在myeclipse 上建立一个web工程,并将
1).axis-bin-1.4\axis-1_4\lib 下的所有包拷贝到axis_example/WEB-INF/lib目录下,
2).axis-bin-1.4\axis-1_4\webapps\axis\WEB-INF下的web.xml文件拷贝到axis_example/WEB-INF目录下。
2.编写需要发布的服务类,并发布服务。
``1).在axis_example/src下新建一MyServic.java文件,内容为:
public class MyService {
public String processService(String arg){
return arg;
}
}
2).在src同级目录下建立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="MyService"/>
<parameter name="allowedMethods" value="processService"/>
</service>
</deployment>
3)部署web工程,启动tomcat
4)在axis_example/WEB-INF目录下执行:
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis_example/servlet/AxisServlet deploy.wsdd
执行后可看到在axis_example/WEB-INF目录下生成server-config.wsdd文件。
5)重新启动tomcat ,以便加载 server-config.wsdd 文件。
6)在src下创建 Client.java 内容为:
public static void main(String [] args) throws Exception {
// 指出service所在URL
String endpoint = "http://localhost:" + "8080" + "/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);
}
7) 编译Client.java,运行其中的main方法进行测试,可以看到屏幕打印出:" This is Dynamic Proxy test!"
3.使用WSDL2JAVA生成客户端使用的桩
axis 提供了wsdl2java工具,web service服务器端提供了一个地址,可以访问到WSDL文件,wsdl2java工具格式为:java org.apache.axis.wsdl.WSDL2Java [options] WSDL-URI
java -Djava.ext.dirs= E:\project\axis_example\WEB-INF\lib org.apache.axis.wsdl.WSDL2Java http://localhost:8081/axis_example/services/MyService?wsdl -p test.mytest -o E:\project\axis_example\src
生成相应的客户端java文件。
参数
-p 指定生成的java文件包名
-o 指定生成的java文件输出目录
如果不指定包名,axis会根据命令参数 WSDL-URI 生成相应的包名,如localhost\axis_example\jws\MyService_jws
上述命令会在 E:\project\axis_example\src\test\mytest 目录下生成四个文件:
MyServiceSoapBindingStub.java (相当于上面的MyService.java)
MyService_PortType.java (相当于上面的MyServiceInterface.java)
MyServiceService.java/MyServiceServiceLocator.java (Service Locator模式,隐藏了具体的业务逻辑)
编写junit单元测试,在axis_example\src\test\mytest下新建一TestClient.java文件(拷贝junit.jar包到axis_example/WEB-INF目录下),内容为:
package test.mytest;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import junit.framework.Test;
public class TestClient extends TestCase {
public TestClient(String string) {
super(string);
}
public void MyServiceClient() throws Exception {
MyServiceService service = new MyServiceServiceLocator();
MyService_PortType client = service.getMyService() ;
String ret = client.processService("This is Junit Test!");
System.out.println(ret);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestClient("MyServiceClient"));
return suite;
}
}
编译上面四个service文件,并编译运行 TestClient.java ,看到屏幕打印出:" This is Junit Test!"
webservice AXIS实现实例
最新推荐文章于 2024-09-28 04:38:12 发布