转载:axis1.4学习笔记
1.下载axis1.4
下载地址:http://axis.apache.org/axis/ 选择Axis 1.4 Final,下载axis-bin-1_4.zip即可。
2.准备工作
把下载的axis中的jar包放到web项目中,至少需要如下包:
?
axis.jar
saaj.jar
wsdl4j-1.5.1.jar
jaxrpc.jar
commons-discovery-0.2.jar
web.xml中添加如下代码:
?
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
3.编写服务端代码
web项目发布目录下(我的是WebContent目录)创建文件夹services,services文件夹下创建文件SayHello.jws,内容如下:
?
public class SayHello {
public String getName(String name)
{
return "hello," + name;
}
}
访问该服务是否正常 http://localhost:8080/proj1/services/SayHello.jws
访问后会自动创建对应的目录和class文件 WEB-INF/jwsClasses/services/SayHello.class
4.编写客户端代码
?
String endPoint = "http://localhost:8080/proj1/services/SayHello.jws";
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName(new QName(endPoint,"getName"));
call.setTargetEndpointAddress(new URL(/blog_article/endPoint/index.html));
String result = (String)call.invoke(new Object[]{"abcddd"});
System.out.println(result);
执行该代码打印内容为:hello,abcddd
-----------------------------------------
通过上篇文章,已经可以通过axis1.4发布服务以及调用服务了,不过上篇文章发布服务使用的方法不够灵活,这里介绍一种灵活的方式
一、以Stubs的方式发布服务
1.以这种方式发布还需要两个J2EE包
2.修改web.xml,我使用了struts2,和axis的配置会有冲突,为了避免冲突,对struts2的相关配置也做了对应修改,这里贴出完整web.xml文件内容:
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>static</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/struts/dojo/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
3.编写服务类 com.site.webservice.MyService.java
?
package com.site.webservice;
public class MyService{
public String getName(String name)
{
return "hi," + name;
}
}
4.在项目WEB-INF目录下创建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.site.webservice.MyService" />
<parameter name="allowedMethods" value="*" />
</service>
</deployment>
5.在项目WEB-INF目录下创建deploy.bat文件,内容为:
?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8080/proj1/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd
dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行deploy.bat,执行成功后会提示:
?
Processing file deploy.wsdd
<Admin>Done processing</Admin>
通过http://localhost:8080/proj1/services可以检查服务是否发布成功
6.编写客户端代码调用我们创建的服务:
?
String endPoint = "http://localhost:8080/proj1/services/MyService";
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName(new QName(endPoint,"getName"));
call.setTargetEndpointAddress(new URL(/blog_article/endPoint/index.html));
String result = (String)call.invoke(new Object[]{"abcd"});
System.out.println(result);
执行该代码打印内容为:hi,abcd
7.如果需要卸载MyService服务,在WEB-INF目录下创建undeploy.wsdd文件:
?
<undeployment xmlns="http://xml.apache.org/axis/wsdd/">
<service name="MyService"/>
</undeployment>
在WEB-INF目录下创建undeploy.bat文件:
?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8080/proj1/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% undeploy.wsdd
dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行undeploy.bat,MyService服务就被卸载了
二、以Stubs的方式调用服务
1、在WEB-INF目录下创建client.bat文件:
?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=D:\workspace\proj1\src
set Package=com.site.client
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% http://localhost:8080/proj1/services/MyService?wsdl
dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行client.bat,会在com.site.client下自动生成如下文件类:
MyServiceSoapBindingStub.java
SayHello.java
SayHelloService.java
SayHelloServiceLocator.java
2、客户端调用
?
SayHelloService service = new SayHelloServiceLocator();
SayHello client = service.getMyService() ;
String result = client.getName("abcdefg");
System.out.println(result);