Axis1.4开发webservice
SOAP 是基于 XML 的简易协议,SOAP 即Simple Object Access Protocol(简单对象访问协议),可使应用程序在 HTTP 之上进行信息交换。 或者更简单地说: SOAP 是用于访问网络服务的协议。 SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信 |
目的:本节主要用于介绍使用axis开发webservice,包括服务端的创建、webservice的部署、客户端的调用
1.准备工作:
概念:SOAP(简单对象访问协议)、WSDL(web服务描述语言)、XML(可扩展标记语言)、axis(阿帕奇可扩展交互系统)
(1) 下载axis1.4,将lib下的jar文件拷贝到工程的WEB-INF\lib下
(2) 配置Java和Tomcat环境,设置axis环境,例如:
set AXIS_HOME= C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\axis set AXIS_LIB=%AXIS_HOME%\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
(3) 配置web.xml添加
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Apache-Axis</display-name> <listener> <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> </servlet> <servlet> <servlet-name>AdminServlet</servlet-name> <display-name>Axis Admin Servlet</display-name> <servlet-class> org.apache.axis.transport.http.AdminServlet </servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet> <servlet-name>SOAPMonitorService</servlet-name> <display-name>SOAPMonitorService</display-name> <servlet-class> org.apache.axis.monitor.SOAPMonitorService </servlet-class> <init-param> <param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value> </init-param> <load-on-startup>100</load-on-startup> </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>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping <servlet-name>SOAPMonitorService</servlet-name> <url-pattern>/SOAPMonitor</url-pattern> </servlet-mapping> <session-config> <session-timeout>5</session-timeout> </session-config> <mime-mapping> <extension>wsdl</extension> <mime-type>text/xml</mime-type> </mime-mapping> <mime-mapping> <extension>xsd</extension> <mime-type>text/xml</mime-type> </mime-mapping> <welcome-file-list id="WelcomeFileList"> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.jws</welcome-file> </welcome-file-list> </web-app>
|
(4)Axis 支持三种web service开发方式,分别为:
1 、Dynamic Invocation Interface ( DII)
2 、Dynamic Proxy方式
3 、Stubs方式
这里只讲解最常用的Stubs方式
2.实例演示
1.写一个方法类,以bean作为参数
//示例:以短信服务为例
package com.my.server;
public class RequestMessage { private String from;
private String to;
private String content;
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getFrom() { return from; }
public void setFrom(String from) { this.from = from; }
public String getTo() { return to; }
public void setTo(String to) { this.to = to; } } |
package com.my.server;
import org.omg.CORBA.Request;
public class ResultResponse { //发送结果代码 private int code;
private String errorText;
//发送的原始信息 private RequestMessage message;
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public String getErrorText() { return errorText; }
public void setErrorText(String errorText) { this.errorText = errorText; }
public RequestMessage getMessage() { return message; }
public void setMessage(RequestMessage message) { this.message = message; }
} |
package com.my.server;
public class SendService { /*** 短信息发送Web服务接口*/ public ResultResponse send(RequestMessage msg) throws Exception { System.out.println("from:" + msg.getFrom()); System.out.println("to:" + msg.getTo()); System.out.println("CONTENT:" + msg.getContent()); ResultResponse res = new ResultResponse(); res.setMessage(msg);
String contentString=msg.getContent(); if(contentString.length()>10) { res.setCode(0); res.setErrorText("发送内容不超过10个字符!"); } else { res.setCode(0); res.setErrorText("ok"); }
return res; } } |
2. 编写deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="sendService" provider="java:RPC"> <parameter name="className" value="com.my.server.SendService" /> <parameter name="allowedMethods" value="*" /> <beanMapping qname="myNS:RequestMessage" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.my.server.RequestMessage" /> <beanMapping qname="myNS:ResultResponse" xmlns:myNS="urn:BeanService" languageSpecificType="java:com.my.server.ResultResponse" /> </service> </deployment> |
3.编写undeploy.wsdd
<undeployment xmlns="http://xml.apache.org/axis/wsdd/"> <service name="sendService"/> </undeployment>
|
4.发布接口(先启动tomcat)
进入到tomcat的WEB-INF目录下,执行命令进行发布。会在此目录下生成
server-config.wsdd文件(以后可以在此目录配置其他接口)
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://10.166.43.110:8080/zhangyiAxis/servlet/AxisServlet deploy.wsdd |
或者编写一个批处理文件:deploy.bat
set Axis_Lib=F:\webservice\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Axis_Servlet=http://10.166.43.110:8080/zhangyiAxis/servlet/AxisServlet %Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% D:\apache-tomcat-6.0.20\apache-tomcat-6.0.20\webapps\zhangyiAxis\WEB-INF\deploy.wsdd @echo 文件server-config.wsdd已经生成 @echo ----------------------------------------- @pause |
4.1下面命令用于取消一个已经发布的服务
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://10.166.43.110:8080/zhangyiAxis/servlet/AxisServlet undeploy.wsdd |
或者编写一个批处理文件:undeploy.bat
set Axis_Lib=F:\webservice\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Axis_Servlet=http://10.166.43.110:8080/zhangyiAxis/servlet/AxisServlet %Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% D:\apache-tomcat-6.0.20\apache-tomcat-6.0.20\webapps\zhangyiAxis\WEB-INF\undeploy.wsdd @echo 服务已经取消 @echo ----------------------------------------- @pause |
5.重启tomcat,访问工程目录,如:http://10.166.43.110:8080/zhangyiAxis/servlet/AxisServlet
(其中zhangyiAxis为工程名)
会看到新发布的webservice接口sendService,点击进入此接口的wsdl内容;
复制此页面内容,另存为如sendService.wsdl,用记事本等工具打开去掉开头的空格,替换去掉“-”;
6.可以经此文件sendService.wsdl放到一个,使用wsdl于批处理生成java客户端的目录:如我的目录F:\webservice 【也可以都放到WEB-INF目录下】
其中 wsdl生成java客户端文件.bat 为
set Axis_Lib=F:\webservice\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=F:\webservice\Testclient set Package=com.my.client %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% F:\webservice\sendservice.wsdl @echo 客户端代码已经成功生成,代码在src目录下 @echo ----------------------------------------- @pause |
重启tomcat,执行“wsdl生成java客户端文件.bat”会生成文件夹Testclient,
打开复制com文件夹,到工程的src下即可
//Bean参数 ResultResponse.java //自动生成 SendService.java 定义了Web服务接口,此例中只有一个send方法。
|
7.写一个测试
package com.my.client.test;
import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.my.client.RequestMessage; import com.my.client.ResultResponse; import com.my.client.SendService; import com.my.client.SendServiceService; import com.my.client.SendServiceServiceLocator;
public class Test {
public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException { /* SendServiceService为接口; SendServiceServiceLocator为接口实现类 */ SendServiceService service = new SendServiceServiceLocator();
//调用接口中提供的服务方法 SendService client = service.getsendService();
//设置要使用的接口服务地址:多用于动态调用不同服务器上的接口 URL portAddress=new URL("http://localhost:8080/zhangyiAxis/services/sendService"); SendService client2 = service.getsendService(portAddress);
//返回接口服务完整地址 String addr = service.getsendServiceAddress(); System.out.println(addr);
//传参数bean RequestMessage msg = new RequestMessage(); msg.setContent("hello!"); msg.setFrom("www.qq.com"); msg.setTo("www.baidu.com"); //接收返回的结果 ResultResponse resp = client.send(msg); RequestMessage msgRec = resp.getMessage();
System.out.println("From:" + msgRec.getFrom() + "\nto :" + msgRec.getTo() + "\ncontent: " + msgRec.getContent() + "\ncode: " + resp.getCode() + "\nerror code : " + resp.getErrorText());
}
}
|
补充:批处理
WSDL生成服务端JAVA代码.bat
@echo ----------------------------------------- @echo 设置Output_Path设置为你要生成的路径 @echo Package设置为生成文件的包路径 @echo 最后是wsdl可以是文件路径 可以是URL @echo WSDL2Java -o [放在那个包下面] -d Session -s -S true [wsdl路径] @echo ----------------------------------------- @echo 切换到当前目录 set CURRENT_DIR=%cd% @echo %CURRENT_DIR% cd %CURRENT_DIR% set Axis_Lib=F:\webservice\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=F:\webservice\AsServerClient set Package=com.huawei.asserver %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% -s -S true F:\webservice\sysMessageEx2.wsdl @echo 服务端代码已经成功生成,代码在src目录下 @echo ----------------------------------------- @pause |
java文件生成wsdl文件.BAT
set Axis_Lib=F:\webservice\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=F:\user.wsdl set Package=com.test.user set namespace=urn:userservice set url=http://localhost:8080/AxisTest2/services/UserService %Java_Cmd% org.apache.axis.wsdl.Java2WSDL -o%Output_Path% -l%url% -n%namespace% -p%Package%=%namespace% -y rpc com.test.service.UserService @echo wsdl已经成功生成 @echo ----------------------------------------- @pause |
Regular Invocation Example
URL url = new URL(“http://localhost:8080/wsrf/services/MyService"); MyServiceAddressingLocator locator = new MyServiceAddressingLocator(); MyService port = locator.getMyServicePort(url); port.hello();
|
根据WSDL生成服务端代码,(参考批处理文件)
生成的文件如下:
分析所生成的文件,服务器端比客户端多了一个PersonManageSoapBindingImpl.java的文件,该文件就是服务端的实现类文件。把生成服务端的代码加到工程里,相关的文件已被修改为支持远程SOAP调用。接下来我们可以在上述的实现类中添加逻辑就可以了。
修改后的代码如下:
UserManageSoapBindingImpl.java
/** * UserManageSoapBindingImpl.java * * This file was auto-generated from WSDL * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter. */
package com.my.server;
public class UserManageSoapBindingImpl implements com.my.server.UserManageInter { public void addUser(com.my.server.UserBean userBean) throws java.rmi.RemoteException { //此处为新增的业务 System.out.print("Info: ***** userBean="+userBean.toString()+"ID ="+userBean.getID()); System.out.println("Name = "+userBean.getName()+"Address ="+userBean.getAddress());
}
}
参考文档《WEB SERVICE开发指南.pdf》 |