portTypename="SayHello">
-operationname="hello"parameterOrder="name">
inputmessage="impl:
helloRequest"name="helloRequest"/>
outputmessage="impl:
helloResponse"name="helloResponse"/>
operation>
portType>
-bindingname="SayHelloSoapBinding"type="impl:
SayHello">
bindingstyle="rpc"transport="http:
//schemas.xmlsoap.org/soap/http"/>
-operationname="hello">
operationsoapAction=""/>
-inputname="helloRequest">
bodyencodingStyle="http:
//schemas.xmlsoap.org/soap/encoding/"namespace="http:
//DefaultNamespace"use="encoded"/>
input>
-outputname="helloResponse">
bodyencodingStyle="http:
//schemas.xmlsoap.org/soap/encoding/"namespace="http:
//127.0.0.1:
8080/MyWebServices/services/SayHello"use="encoded"/>
output>
operation>
binding>
-servicename="SayHelloService">
-portbinding="impl:
SayHelloSoapBinding"name="SayHello">
addresslocation="http:
//127.0.0.1:
8080/MyWebServices/services/SayHello"/>
port>
service>
definitions>
说明:
wsdl中描述了该webservices的方法为:
hello,输入参数为name,String类型,返回参数为String类型,调用方式为rpc,和我们之前在wsdd中配置的一样。
看完说明我们可以直接在浏览器中调用该webservices:
在浏览器中输入:
http:
//127.0.0.1:
8080/MyWebServices/services/SayHello?
method=hello&name=waiwai
你会看到:
xmlversion="1.0"encoding="UTF-8"?
>
-Envelopexmlns:
soapenv="http:
//schemas.xmlsoap.org/soap/envelope/"xmlns:
xsd="http:
//www.w3.org/2001/XMLSchema"xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance">
-Body>
-encodingStyle="http:
//schemas.xmlsoap.org/soap/encoding/">
type="soapenc:
string"xmlns:
soapenc="http:
//schemas.xmlsoap.org/soap/encoding/">你好,waiwai,axisVer1.4欢迎你.
Body>
Envelope
我们可以看到浏览器返回的是一String类型数据:
你好,waiwai,axisVer1.4欢迎你.
7.接下来我们使用外部项目调用该webservices:
创建新的项目CallWebServices,同样引入axis的8个jar包,创建调用webservices的类
示例代码:
importjavax.xml.namespace.QName;
importorg.apache.axis.client.Call;
importorg.apache.axis.client.Service;
publicclassCallSayHello{
publicstaticvoidmain(String[]args){
try{
Stringendpoint="http:
//127.0.0.1:
8080/MyWebServices/services/SayHello?
wsdl";
//webservices的wsdl的url地址
Serviceservice=newService();
//创建服务
Callcall=(Call)service.createCall();
//创建调用
call.setTargetEndpointAddress(endpoint);
//设置调用的url地址
//call.setOperationName("hello");
call.setOperationName(newQName("hello"));
//设置调用的方法(method)
//call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
//设置获取响应参数的格式
//call.setUseSOAPAction(true);
//call.setSOAPActionURI("
Stringresult=(String)call.invoke(newObject[]{"waiwai"});
//调用Webservice
System.out.println("resultis:
"+result);
}
catch(Exceptione){
System.err.println(e.toString());
}
}
}
执行该程序:
后台输出:
resultis:
你好,waiwai,axisVer1.4欢迎你.
这表示webservices调用成功。
8.实际应用中webservices的例子
调用天气预报的webservices:
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.UnsupportedEncodingException;
import.MalformedURLException;
import.URL;
import.URLConnection;
importjava.util.ArrayList;
importjava.util.List;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.DOMException;
importorg.w3c.dom.Document;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;
importorg.xml.sax.SAXException;
/**@srchttp:
//eric-
publicclassCallWeatherWebServices{
privatestaticStringSERVICES_HOST="";
privatestaticStringWEATHER_SERVICES_URL="
privatestaticStringPROVINCE_CODE_URL=WEATHER_SERVICES_URL
+"getRegionProvince";
privatestaticStringCITY_CODE_URL=WEATHER_SERVICES_URL
+"getSupportCityString?
theRegionCode=";
privatestaticStringWEATHER_QUERY_URL=WEATHER_SERVICES_URL
+"getWeather?
theUserID=&theCityCode=";
privateWeatherUtil(){