最近由于青海的项目要求,需要提供一个外围接口给其他厂商,最后决定用WEBSERVICE的方法给外围提供接口,同时内部用EJB来实现组件封装,这就 涉及到了WEB SERVICE的开发,部署,测试已经EJB的开发,部署,和测验,那么这2样技术我相信很多人喜欢吧,也很想学,是吧,那我就实例开发来讲解吧,先讲 WEBSERVICE的开发,然后我会一节一节的来完善,最终让大家都知道,现在我就将我自己的一些心得写下来。
网络上的相关资料很多,但是很抱歉,我真的很少有按照他们的示范成功过的,也许是我的悟性还不够,好了,废话不讲了,直接开发了。
=一:环境
工具: Eclipse 3.2 +MyEclipse 5.0.0.
JDK是1.4,相应的JAR包:axis.jar axis-ant.jar等,这些你必须自己准备好,呵呵
上面的应该您都会吧,不会你可以和我联系tropica@163.com
二:开始
1:先创建一个WEB工程,这个所有人都会的,不要我多讲。工程名称为:CallSmsService
2:替换相与的配置文件,这一步主要完成相关SERVELET的配置,找到WEB-INF下面的WEB.XML文件,然后加上下面的内容
<listener>
<listener-class>
org.apache.axis.transport.http.AxisHTTPSessionListener
</listener-class>
</listener>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-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>
<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>
<!-- uncomment this if you want the admin servlet -->
<!--
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
-->
<session-config>
<!-- Default to 5 minute session timeouts -->
<session-timeout>5</session-timeout>
</session-config>
<!-- currently the W3C havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
for now we go with the basic 'it's XML' response -->
<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>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
3:上面的完成后,请您发布WEB应用,测验刚才的SERVELET是不是已经发布成功,访问地址:
http://localhost:7001/CallSmsService/servlet/AxisServlet 如果正确的话,会出现下面的内容:
And now... Some Services
如果没有出现上面的,那请你自己仔细检查原因,不知道原因的,哈哈,按照我的经验,错误应该是你发布的时候错了,没有搞清楚上下文,那你得继续好好学习WEB了。。
4:上面的OK了,好了,开始我们的核心的东东了,编辑deploy.wsdd,定义接口,将deploy.wsdd拷贝到工程应用的lib目录下,注意lib就是库文件,一般位于WebRoot\WEB-INF\lib下面,deploy.wsdd的内容为:
<!-- Use this file to deploy some handlers/chains and services -->
<!-- Two ways to do this: -->
<!-- java org.apache.axis.client.AdminClient deploy.wsdd -->
<!-- after the axis server is running -->
<!-- author xuzr@ -->
<!-- java org.apache.axis.utils.Admin client|server deploy.wsdd -->
<!-- from the same directory that the Axis engine runs -->
<deployment
xmlns="http://xml.apache.org/axis/wsdd/ "
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java ">
<!-- Services from HelloImplService WSDL service -->
<service name="CallSoapImpl" provider="java:RPC" style="rpc" use="encoded">
<parameter name="wsdlTargetNamespace" value="http://localhost:7001/CallSmsService/services/CallSoapImpl"/ >
<parameter name="wsdlServiceElement" value="CallSoap"/>
<parameter name="wsdlServicePort" value="CallSoapImpl"/>
<parameter name="className" value="com.linkage.soap.CallSoapImpl"/>
<parameter name="wsdlPortType" value="CallSoapImpl"/>
<parameter name="typeMappingVersion" value="1.2"/>
<parameter name="allowedMethods" value="*"/>
<beanMapping languageSpecificType="java:com.linkage.soap" qname="ns1:CallSoapImpl" xmlns:ns1="urn:BeanService"/>
</service>
</deployment>
里面的相关东西我解释下,CallSoapImpl是你的逻辑实现类,CallSoap是接口,如果你不明白,没有关系,下面我会讲相关的类,你就会明白了。上面的配置好了,就把这个文件放到LIB下面吧
5:发布接口,发布前要设计环境变量哦,我这里的为:
set classpath=E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\axis.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\commons-discovery-0.2.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\commons-logging.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\jaxrpc.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\saaj.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\wsdl4j-1.5.1.jar;E:\beaapp\mydomain\applications\CallSmsService\WEB-INF\lib\j2ee.jar;$classpath
细 心的你就会发现,我这里的就是开始准备里的包,有的包你没有,自己去DOWNLOAD吧,网络这么发达,你表说你不知道怎么下。然后执行命令:java org.apache.axis.client.AdminClient -lhttp://localhost:7001/CallSmsService/services/AdminService deploy.wsdd 注意哦,这一补重要呢,发布成功的话,会出现这样的提示信息:
Processing file deploy.wsdd
<Admin>Done processing</Admin>
6:继续访问:http://localhost:7001/CallSmsService/servlet/AxisServlet 会多出一个节点,为:
And now... Some Services
这个就是发表成功的哦,点下CallSoapImpl这个节点,会发现有这样的提示:
CallSoapImpl
Hi there, this is an AXIS service!
Perhaps there will be a form for invoking the service here...
同时会出现最关键的东西,就是我们提供给外围的,见下面:
<? xml version="1.0" encoding="UTF-8" ?>
WSDL created by Apache Axis version: 1.2.1--> < type="text/javascript">f(clean);
Built on Aug 08, 2005 (11:49:10 PDT)
如果出现上面的内容,那么就恭喜你,发布成功了,到这里为止,你只是发布成功了,可以给外围使用的,但是你要 测验自己的是不是正确,怎么办呢,OK,下一节我继续讲。