MyEclipse下开发Web Service(Axis)

MyEclipse下开发Web Service(Axis)【转载】

AXIS1.4 发布Web Service及客户端测试 

1. 在Myeclipse中新建 Web project,如 WS1.4

2. src中创建服务的操作类, axisJWS.java 内容如下:
package com.server;

public class axisJWS {
  static int index=1;
     public String method(String userAuth, String xml){
      System.out.println("流水号第"+index+++"行");
      return ">>>"+userAuth+"=="+xml;
     }
}

3. 在WebRoot\WEB-INF\下增加文件server-config.wsdd 用于发布服务。
<?xml version="1.0" encoding="UTF-8"?>
<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="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="axis.sendMinimizedElements" 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"/>

 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 
 <service name="axisJWS" provider="java:RPC">
  <parameter name="allowedMethods" value="*"/>
  <parameter name="scope" value="Request"/>
  <parameter name="className" value="com.server.axisJWS"/>
 </service>

 
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
</deployment>

4. web.xml 内容修改为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3.xsd">
  <!-- 第一段 -->
  <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>SOAPMonitorService</servlet-name>
    <display-name>SOAP Monitor Service</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>
  </servlet>
  
  <!-- 第二段 -->
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/WS/SOAPMonitor</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/WS/servlet/AxisServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


5. 部署整个Web Project到Tomcat6.0.14.

6. 启动tomcat6.0.14. 通过
http://127.0.0.1:8080/WS1.4/WS/servlet/AxisServlet可访问到服务, 超链接查看服务(wsdl)
http://127.0.0.1:8080/WS1.4/services/axisJWS?wsdl。

下面开始做客户端测试(需要加入axis1.4的*.jar)
测试文件
package com.server.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

public class axisSoap {

 public static void main(String[] args) {
  try {     
            String wsdlUrl = "http://127.0.0.1:8080/WS1.4/services/axisJWS?wsdl";   
            String soapActionURI = "http://127.0.0.1:8080/WS1.4/services/axisJWS?wsdl";   
            // 创建调用对象   
            Service service = new Service();   
            Call call = (Call) service.createCall();   
            // 调用getUserInfo   
            System.out.println(">>>调用开始: ");   
            //xmlns   
            call.setOperationName(new QName("http://127.0.0.1:8080/WS1.4/services/axisJWS?wsdl", "method"));   
            call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));   
               
            //parameter & return   
            call.addParameter("userAuth", org.apache.axis.encoding.XMLType.XSD_STRING,   
                    javax.xml.rpc.ParameterMode.IN);   
            call.addParameter("xml", org.apache.axis.encoding.XMLType.XSD_STRING,   
                    javax.xml.rpc.ParameterMode.IN);   
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);   
               
            //set soap action   
            call.setUseSOAPAction(true);   
            call.setSOAPActionURI(soapActionURI);   
            //call   
            String ret = (String) call.invoke(new Object[] { "参数一" , "参数二" });   
            System.out.println("返回值: " + ret);   
          } catch (Exception e) {   
            e.printStackTrace();   
          }
 }
}

测试结果:
服务端:
流水号第1行
流水号第2行
流水号第3行
流水号第4行
客户端:
>>>调用开始: 
返回值: >>>参数1==Param-1
返回值: >>>参数2==Param-2
返回值: >>>参数3==Param-3
返回值: >>>参数4==Param-4

测试完成。

你可能感兴趣的:(MyEclipse下开发Web Service(Axis))