`

Webservice开发和使用指南3

阅读更多

 

8.Axis集成Resin或其它应用服务器
8.1    如果配置和发布
Axis是以Servlet的方式运行的,而Resin的作用相当于Servlets容器(Container),因此只要配置得当,就可以使AxisResin环境中运行,这一点也适用于Resin以外的其它应用服务器。在Resin中配置Axis的方法如下。
axis-1_1/webapps/axis/WEB-INF/web.xml中的Servlet配置项复制到resin.conf中对应的Web应用程序配置中。通常应包括以下内容。
一个servlet可以配置多个在url地址拦访问的方式.

 <!-- Axis Web-Service Configuration Start -->
 <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>/axis/servlet/AxisServlet</url-pattern>
 </servlet-mapping>
访问方式:http:// localhost:8080/projectName/axis/servlet/AxisServlet
 <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
 </servlet-mapping>
访问方式:http:// localhost:8080/projectName/*.jws
 <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>/axis/servlet/AdminServlet</url-pattern>
   </servlet-mapping>
 -->
 <!-- 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>
 
   <!-- Axis Web-Service Configuration End -->

 

 

 

 

 

8.2    平台的配置方式
    <servlet>
       <servlet-name>axis</servlet-name>     <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
       <load-on-startup>5</load-on-startup>
    </servlet>
<servlet-mapping>
       <servlet-name>axis</servlet-name>
       <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
8.3    Axis提供了哪些开发工具
Apache Axis提供了WSDL2JavaJava2WSDL两个开发工具。
WSDL2Java利用已知的WSDL文件生成服务端和客户端代码。该WSDL文件可以是由合作伙伴提供的,也可以是利用Java2WSDL生成的。Java2WSDL根据已有的Java类文件生成WSDL文件,Java类文件可以是接口类文件,并不需要实现细节。
此外Axis还提供了SoapMonitorAppletTCPMon工具,可用于监测Web服务。
8.4    生成Web服务的服务端和客户端代码
8.4.1             生成或取得WSDL文件
Java2WSDLAxis提供的利用Java类文件得到WSDL文件的工具。类文件可以使用接口文件编译生成,例如下面的接口文件SoftwarePrice.java

package samples.userguide.example6;
/**
 * Interface describing a web service to set and get software prices.
 **/
public interface SoftwarePrice {
    public void setWidgetPrice(String softWareName, String price);
    public String getWidgetPrice(String softWareName);

编译SoftwarePrice.java

javac SoftwarePrice.java

SoftwarePrice.class复制到正确的package路径下。
执行下面的命令:

java org.apache.axis.wsdl.Java2WSDL -o sp.wsdl -l "http://test.com:80/services/SoftwarePrice" -n "urn:SoftwarePrice"
 -p"samples.userguide.example6" "urn:Example6" samples.userguide.example6.SoftwarePrice

各参数的含义如下。

-o:指定输出的WSDL文件的文件名。
-l:指定服务的位置。
-n:WSDL文件的目标名字空间。
-p:指定从package到名字空间的映射,这里可以有多个映射。

最后面的类文件包含了Web服务的接口。
该命令执行后,将生成sp.wsdl文件。
如果按CLASSPATH的设置找不到指定的类文件,Axis将报告异常,如下所示。

java.lang.ClassNotFoundException: samples.userguide.example6.SoftwarePrice
        at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
        at java.security.AccessController.doPrivileged(Native Method)
 ……

如果出现上面的问题,请检查是否已将有关类文件复制到正确的位置或CLASSPATH设置是否正确。
生成WSDL文件以后,就可以利用Axis提供的WSDL2Java工具生成Web服务的服务端代码和客户端代码了。
8.4.2            注意事项
WSDL文件也可以由合作伙伴提供。这种情况下合作伙伴往往是Web服务的提供者或标准接口的制定者,开发者只要按照既定的WSDL文件生成客户端或服务端代码就可以了。
8.4.3            生成客户端或服务端代码
WSDL2Java工具用于从WSDL文件生成客户端存根(stub)代码,服务端框架(skeleton)代码以及WSDL中的数据类型文件(生成与之对应的Java代码)。开发人员只需向框架代码中补充相关的业务逻辑代码即可得到完整的Web服务代码,因此该工具极大地减轻了开发人员的编码负担。WSDL2Java的使用举例如下。

java org.apache.axis.wsdl.WSDL2Java --server-side --skeletonDeploy true MyService.wsdl

执行上述命令后将生成下列文件。

No.
文件
用途
1.
deploy.wsdd
MyService服务的部署描述文件
2.
MyService.java
MyService服务的接口文件
3.
MyServiceService.java
获得MyService服务的接口文件
4.
MyServiceServiceLocator.java
实现MyServiceService接口
5.
MyServiceSoapBindingImpl.java
实现MyService接口,服务器需要其中补充业务逻辑
6.
MyServiceSoapBindingSkeleton.java
MyService服务的服务端框架代码,实现MyService, org.apache.axis.wsdl.Skeleton接口
7.
MyServiceSoapBindingStub.java
MyService服务的客户端存根代码,实现MyService接口
8.
undeploy.wsdd
注销MyService服务的部署描述文件

2.7.3编写Web服务客户端代码
Web服务的客户端程序完成对Web服务的调用,其程序结构如下。

import com.chinavnet.zx.service.v1_0.*;// WSDL2Java生成的package的名字空间;
public class TestClient {
    public static void main (String[] args) throws Exception{
 com.chinavnet.zx.service.v1_0.SPInterfaceForVNetLocator locator = new SPInterfaceForVNetLocator();//获得一个locator对象
 locator.setMaintainSession(true);
 com.chinavnet.zx.service.v1_0.SPInterfaceForVNetSoap service = locator.getSPInterfaceForVNetSoap();//获得服务对象
 com.chinavnet.zx.service.v1_0.DetailLedgerFeedbackResult feedbackRes = null;
 //初始化Web服务中定义的数据类型
        try {
 feedbackRes = service.generalLedgerFeedback();//调用Web服务的方法并取得返回值
   System.out.println("FeedbackResult :");
   if(feedbackRes != null)
   {
    System.out.println("SPID: " +feedbackRes.getSPID());
    System.out.println("errorDesc: " +feedbackRes.getErrorDescription());
    System.out.println("result: " +feedbackRes.getResult());
   }else
   {
    System.out.println("feedbackRes is null!");
   }
        } catch (java.rmi.RemoteException re) {
 //           throw new junit.framework.AssertionFailedError("Remote Exception caught: " + re );
     re.printStackTrace();
        }
}//End of main()
}//End of TestClient class

测试客户端程序是非常简单的,将客户端程序编译后,执行"java TestClient"即可。
2.8 如何编写服务端代码
MyServiceSoapBindingImpl.java添加相关的业务逻辑代码后,将WSDL2Java生成的源程序编译,打包成jar文件,复制到/usr/local/apache/htdocs/WEB-INF/lib/目录下。
2.9 如何发布Web服务
有了deploy.wsdd文件并准备好类文件后,就可以发布MyService服务了。Axis在安装后自动发布了AdminService,利用它可以发布新的Web服务。方法如下。

java org.apache.axis.client.AdminClient -lhttp://localhost:80/services/AdminService deploy.wsdd
上面的命令执行后,有如下提示。
Processing file deploy.wsdd
<Admin>Done processing</Admin>

9       开发实例
9.1    Axis自带实例设置步骤
To run the address book sample, you must do the following:
1. Set up your CLASSPATH.
2. Generate the Java bindings from the AddressBook.wsdl file.
3. Compile the code.
4. Start a server.
5. Run the sample.
 
1. Set up your CLASSPATH.
    The CLASSPATH must contain: an XML parser (ie., Xerces), JUnit
    (www.junit.org), all the jars in the lib directory, and the directory
    containing the samples subdirectory.
 
2. Generate the Java bindings from the AddressBook.wsdl file.
    a. Change directory to the directory that contains the samples
        subdirectory.
    b. Run the command: java org.apache.axis.wsdl.WSDL2Java -s -d Session
        -Nurn:AddressFetcher2=samples.addr samples\addr\AddressBook.wsdl
 
    This will generate all the bindings, both client-side and server-side,
    into the samples/addr subdirectory.
 
3. Compile the code.
    a. Now you can change directory to samples/addr
    b. And compile the sample: javac *.java
 
4. Start a server.
    To run the sample, you will first need to run a server. To run a very
    simple server you could run, in a separate window:
        java org.apache.axis.transport.http.SimpleAxisServer -p 8080
 
5. Run the sample.
    Finally, to run the client, run testit.sh or testit.cmd, depending on
    your platform.
9.2    Axis开发webservice  
最近的几个项目由于涉及到不同领域 不同技术平台的应用,所以在各个系统的接口部分,采用了webservice技术,来实现不同系统的业务对接.
由于我们采用J2EE平台,下面着重讲下利用axis在J2EE平台开发和部署webservice应用的问题.
下面以一个用户登录的例子介绍开发的过程.
1.       Axis开发包下载.
Apache的一个项目,请下载(略)
2.       编写方法
 
package zpf;
import java.util.*;
import java.io.*; 
public class SSOWebservice
{
   /**
      登陆并得到用户信息
   */
   Public boolean login(String loginid, String password){
      boolean ret=false;
      if(loginid.equals(“zpf”)&&password.equals(“123”))
         ret= true;
      else
         ret=false;
      return ret;
}  
 /**
    得到所有部门信息
*/
 public zpf.DepartmentInfo[] getAllDepts(){
     zpf.Department dept=new zpf.Department();
        zpf.DepartmentInfo[] ret=dept.getAllDepartment();
          return ret;
 }
}
http://localhost:8080/kmwhc/services/SSOWebservice
编译,通过,不在累述.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics