Axis开发webservice

Axis开发webservice

 

最近的几个项目由于涉及到不同领域 不同技术平台的应用,所以在各个系统的接口部分,采用了webservice技术,来实现不同系统的业务对接.

由于我们采用J2EE平台,下面着重讲下利用axisJ2EE平台开发和部署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

编译,通过,不在累述

3.       修改web.xml

加入如下代码

<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>

 

 <!-- uncomment this if you want the admin servlet -->

    <!-- 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>

 

4.       布署

(1)    WEB-INF下建立文件server-config.wsdd

(2)    文件内容如下:

蓝色部分定义了服务的名称,所用的类 方法.

红色部分定义了服务的方法所用的返回值的自定义类型.

<?xml version="1.0" encoding="gb2312"?>

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

      

       <globalConfiguration>

              <parameter name="adminPassword" value="admin"/>

              <parameter name="enableNamespacePrefixOptimization" value="true"/>

              <parameter name="attachments.Directory" value="D:/jakarta-tomcat-5.0.28/webapps/axis/WEB-INF/attachments"/>

              <parameter name="disablePrettyXML" value="true"/>

              <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>

              <parameter name="sendXsiTypes" value="true"/>

              <parameter name="sendMultiRefs" value="true"/>

              <parameter name="sendXMLDeclaration" value="true"/>

              <requestFlow>

                     <handler type="java:org.apache.axis.handlers.JWSHandler">

                            <parameter name="scope" value="session"/>

                     </handler>

                     <handler type="java:org.apache.axis.handlers.JWSHandler">

                            <parameter name="scope" value="request"/>

                            <parameter name="extension" value=".jwr"/>

                     </handler>

              </requestFlow>

       </globalConfiguration>

 

       <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>

       <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>

       <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>

 

<!-- 自定义服务开始 -->

<!-- 单点登陆服务 add by Zhang.P.F -->

 

<service name="SSOWebservice" provider="java:RPC">

  <parameter name="allowedMethods" value="*"/>

  <parameter name="className" value="zpf.SSOWebservice"/>

  <operation name="login" returnType="ns:boolean">

      <parameter name="loginid" type="ns:String"/>

       <parameter name="password" type="ns:String"/>

  </operation>

  <operation name="getAllDepts" returnType="ns:ArrayOf_tns2_DepartmentInfo">

   </operation>

 <typeMapping

        xmlns:ns="http://interf.kmwhc.zpf.com"

        qname="ns:DepartmentInfo"

        type="java:zpf.DepartmentInfo"

        serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"

        deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"

  />

  <typeMapping

        xmlns:ns="urn:SSOWebservice"

        qname="ns:ArrayOf_tns2_DepartmentInfo"

        type="java:zpf.DepartmentInfo[]"

        serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"

        deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"

        encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

  />

 </service>

 

<!-- 自定义服务结束 -->

 

       <service name="AdminService" provider="java:MSG">

              <parameter name="allowedMethods" value="AdminService"/>

              <parameter name="enableRemoteAdmin" value="false"/>

              <parameter name="className" value="org.apache.axis.utils.Admin"/>

              <namespace>http://xml.apache.org/axis/wsdd/</namespace>

              <namespace>http://xml.apache.org/axis/wsdd/</namespace>

       </service>

 

       <service name="Version" provider="java:RPC">

              <parameter name="allowedMethods" value="getVersion"/>

              <parameter name="className" value="org.apache.axis.Version"/>

       </service>

 

       <transport name="http">

              <requestFlow>

                     <handler type="URLMapper"/>

                     <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>

              </requestFlow>

              <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>

              <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>

              <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>

              <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>

              <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>

              <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>

       </transport>

 

       <transport name="local">

              <responseFlow>

                     <handler type="LocalResponder"/>

              </responseFlow>

       </transport>

</deployment>

 

文件修改完毕后,重新启动你的应用服务器.

5.       测试

  IE里输入http://localhost:8080/kmwhc/services/SSOWebservice

看到页面提示如下信息表示布署成功:

 

SSOWebservice

Hi there, this is an AXIS service!

Perhaps there will be a form for invoking the service here...

 

6.     编译生成客户端调用包

   我用的ANT进行编译,在配置文件里加入如下代码

<!--登录服务-->

        <java2wsdl output="${client.dir}/SSOWebservice.wsdl"

            className="zpf.SSOWebservice"

            namespace="urn:SSOWebservice"

            location="http://localhost:8080/kmwhc/services/SSOWebservice">

            <mapping namespace="urn:SSOWebservice"

                package="zpf.client" />

        </java2wsdl>

        <wsdl2java url="${client.dir}/SSOWebservice.wsdl"

            output="${client.dir}" deployscope="session" serverSide="yes"

            skeletonDeploy="no" noimports="no" verbose="no" testcase="no">

            <mapping namespace="urn:SSOWebservice"

                package="zpf.client" />

        </wsdl2java>

 

编译,生成wsdl文件和客户端代码.

 

7调用

 

调用代码:

zpf.client.SSOWebserviceServiceLocator sv=new zpf.client. SSOWebserviceServiceLocator();

zpf.client.SSOWebservice ssp=sv.getWebCaseservice(new java.net.URL("http://localhost:8080/kmwhc/services/SSOWebservice"));

   boolean aa=ssp.login(“admin”,”123”);

阅读更多
个人分类: J2EE研究
上一篇Spring学习(1)
下一篇判断页面CHECKBOX选中的个数
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭