WebService服务的实现及调用

eclipse下新建webapp项目

此WebService服务由Axis实现,样例项目所需要jar包及目录:

Web.xml配置

<?xml version="1.0" encoding="utf-8"?>

<!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>
    
  
<!--实际servlet程序,这里是AxisServlet-->
  
<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和url的对应关系-->
  
<servlet-mapping>
    
<servlet-name>AxisServlet</servlet-name>
    
<url-pattern>/services/*</url-pattern>
  
</servlet-mapping>


</web-app>

WebService服务:

package com.archie.service;

/**
 * 服务MyService
 * 
@author archie
 *
 * since 2011-7-13 下午02:34:57
 
*/
public class SayHelloService {

    
/**
     * 输出List集中字符串
     * 
@return
     
*/
    
public String SayHello2WS(String name){
        
return "Hello,WebService!"+"_____by"+"\t"+name;
    }
}

WebService服务的配置(server-config.xml)

server-config.xml(WEB-INF目录下)

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
    xmlns:java
="http://xml.apache.org/axis/wsdd/providers/java"
    xmlns:handler
="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">
    
<globalConfiguration name="GlobalConfiguration1" type="" regenerateElement="false">
        
<requestFlow name="RequestFlow1" type="" regenerateElement="false">
            
<handler name="Handler1"
                type
="java:org.apache.axis.handlers.JWSHandler" regenerateElement="false">
                
<parameter name="scope" value="session" regenerateElement="false"/>
            
</handler>
            
<handler name="Handler2"
                type
="java:org.apache.axis.handlers.JWSHandler" regenerateElement="false">
                
<parameter name="scope" value="request" regenerateElement="false"/>
                
<parameter name="extension" value=".jwr" regenerateElement="false"/>
            
</handler>
        
</requestFlow>
    
</globalConfiguration>
    
<handler name="URLMapper"
        type
="java:org.apache.axis.handlers.http.URLMapper" regenerateElement="false"/>
    
<handler name="LocalResponder"
        type
="java:org.apache.axis.transport.local.LocalResponder" regenerateElement="false"/>
    
<handler name="Authenticate"
        type
="java:org.apache.axis.handlers.SimpleAuthenticationHandler" regenerateElement="false"/>
    
<transport name="http" type="" regenerateElement="false">
        
<requestFlow name="RequestFlow1" type="" regenerateElement="false">
            
<handler name="Handler1" type="URLMapper" regenerateElement="false"/>
            
<handler name="Handler2"
                type
="java:org.apache.axis.handlers.http.HTTPAuthHandler" regenerateElement="false"/>
        
</requestFlow>
    
</transport>
    
<transport name="local" type="" regenerateElement="false">
        
<responseFlow name="ResponseFlow1" type="" regenerateElement="false">
            
<handler name="Handler1" type="LocalResponder" regenerateElement="false"/>
        
</responseFlow>
    
</transport>
    
<service name="AdminService" type="" regenerateElement="false" provider="java:MSG">
        
<parameter name="allowedMethods" value="AdminService" regenerateElement="false"/>
        
<parameter name="enableRemoteAdmin" value="false" regenerateElement="false"/>
        
<parameter name="className" value="org.apache.axis.utils.Admin" regenerateElement="false"/>
        
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
    
</service>
    
<service name="Version" type="" regenerateElement="false" provider="java:RPC">
        
<parameter name="allowedMethods" value="getVersion" regenerateElement="false"/>
        
<parameter name="className" value="org.apache.axis.Version" regenerateElement="false"/>
    
</service>
    
    
<!-- 配置发布的Service  -->
    
<service name="SayHelloService" provider="java:RPC">
        
<parameter name="className" value="com.archie.service.SayHelloService"/>
        
<parameter name="allowedMethods" value="*" />
    
</service>
        
</deployment>

Tomcat6.0下发布

URL:http://localhost:8080/MyWebService/services/SayHelloService

浏览其wsdl文件

URL:http://localhost:8080/MyWebService/services/SayHelloService?wsdl

wsdl

  <?xml version="1.0" encoding="UTF-8" ?> 
<wsdl:definitions targetNamespace="http://localhost:8080/MyWebService/services/SayHelloService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/MyWebService/services/SayHelloService" xmlns:intf="http://localhost:8080/MyWebService/services/SayHelloService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- 
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)

  
--> 
<wsdl:message name="SayHello2WSResponse">
  
<wsdl:part name="SayHello2WSReturn" type="soapenc:string" /> 
  
</wsdl:message>
<wsdl:message name="SayHello2WSRequest">
  
<wsdl:part name="name" type="soapenc:string" /> 
  
</wsdl:message>
<wsdl:portType name="SayHelloService">
<wsdl:operation name="SayHello2WS" parameterOrder="name">
  
<wsdl:input message="impl:SayHello2WSRequest" name="SayHello2WSRequest" /> 
  
<wsdl:output message="impl:SayHello2WSResponse" name="SayHello2WSResponse" /> 
  
</wsdl:operation>
  
</wsdl:portType>
<wsdl:binding name="SayHelloServiceSoapBinding" type="impl:SayHelloService">
  
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
<wsdl:operation name="SayHello2WS">
  
<wsdlsoap:operation soapAction="" /> 
<wsdl:input name="SayHello2WSRequest">
  
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.archie.com" use="encoded" /> 
  
</wsdl:input>
<wsdl:output name="SayHello2WSResponse">
  
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/MyWebService/services/SayHelloService" use="encoded" /> 
  
</wsdl:output>
  
</wsdl:operation>
  
</wsdl:binding>
<wsdl:service name="SayHelloServiceService">
<wsdl:port binding="impl:SayHelloServiceSoapBinding" name="SayHelloService">
  
<wsdlsoap:address location="http://localhost:8080/MyWebService/services/SayHelloService" /> 
  
</wsdl:port>
  
</wsdl:service>
  
</wsdl:definitions>

该服务的调用:

package com.archie.service.test;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

/**
 * WebService接口测试
 * 
@author archie
 *
 * since 2011-7-13 下午02:06:37
 
*/
public class TestSayHello {

    
public static void main(String[] args) throws Exception {
        
//创建一个服务对象
        Service service = new Service();
        
//创建一个对方法的调用
        Call call = service.createCall();
        
/**
         * 设置调用的终端地址
         * AnotherMethod为server-config.wsdd的Service配置名称
         
*/
        String url 
= "http://localhost:8080/MyWebService/services/SayHelloService";
        call.setTargetEndpointAddress(
                url);
        call.setReturnType(XMLType.XSD_STRING);
        
//设置操作名,即所调用的方法的名称
        call.setOperationName(new QName("SayHello2WS"));
        
//添加参数(参数名,参数类型,参数传递模式)
        call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
        
//调用方法
        String res = (String) call.invoke(new Object[]{"archie"});
        
        System.out.println(
"调用返回结果:"+res);
        
    }
}

运行结果:

posted on 2011-07-14 12:47  archie2010  阅读(684)  评论(0编辑  收藏  举报