技术开发 频道

J2EE应用:WebService服务的实现及调用

  浏览其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);
}
}

  运行结果:

浏览其wsdl文件

0
相关文章