1Webservice基础Webservice跨平台远程调用技术。Webservice是基础http的soap协议,使用xml数据格式传输。Soap=http+xml2webservice的底层通信原理是基于socket的Socket服务端:packagecn.itcast.ws.server;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;/***天气查询socket服务类**@author苗润土**/publicclassWeatherServer{/***@paramargs*@throwsException*/publicstaticvoidmain(String[]args)throwsException{//创建并启动socket服务监听ServerSocketserverSocket=newServerSocket(1234);System.out.println(创建并启动socket服务监听..);while(true){//获得socket链接Socketsocket=serverSocket.accept();//获得socket链接的输入流对象DataInputStreamdataInputStream=newDataInputStream(socket.getInputStream());//获得socket链接的输出流对象DataOutputStreamdataOutputStream=newDataOutputStream(socket.getOutputStream());//接收客户端发送的城市名称StringcityName=dataInputStream.readUTF();//查询天气//....//查询结果StringresultString=雾霾;//返回查询结果dataOutputStream.writeUTF(resultString);}}}Socket客户端packagecn.itcast.ws.client;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.Socket;importjava.net.UnknownHostException;/***天气查询客户端**@authormrt**/publicclassWeatherQueryClient{/***@paramargs*@throwsIOException*@throwsUnknownHostException*/publicstaticvoidmain(String[]args)throwsException{while(true){//创建socket链接Socketsocket=newSocket(192.168.3.100,1234);//获得socket链接的输出流DataOutputStreamdataOutputStream=newDataOutputStream(socket.getOutputStream());//获得socket链接的输入流DataInputStreamdataInputStream=newDataInputStream(socket.getInputStream());//向服务端发送查询天气请求//城市名称StringcityName=北京;//发送数据dataOutputStream.writeUTF(cityName);//获得服务端返回结果StringresultString=dataInputStream.readUTF();System.out.println(cityName+天气查询结果为:+resultString);Thread.sleep(2000);}}}3jax-ws开发(JavaAPIforXML-BasedWebservices)第一步:开发webservice服务类packagecn.itcast.ws.server;importjavax.jws.WebService;importjavax.xml.ws.Endpoint;/***基于jax-ws的webservice服务类**@authormrt**/@WebServicepublicclassWeatherServer{/**查询天气服务方法*/publicStringqueryWeather(StringcityName){//城市名称System.out.println(正在查询+cityName+的天气情况...);//查询结果StringresultString=雾霾;returnresultString;}/***@paramargs*/publicstaticvoidmain(String[]args){//发布webservice服务Endpoint.publish(());}}第二步:查看wsdl使用说明书从下往上看:1服务视图(根)2通过服务视图,bingding到porttype3、通过porttype调用webservice服务方法第三步:使用wsimport生成客户端调用类1、使用wsimport生成webservice的骨架在d:\ws目录会生成客户端调用类第四步:编写客户端代码packagecn.itcast.ws.client;importcn.itcast.ws.server.WeatherServer;importcn.itcast.ws.server.WeatherServerService;/***webservice客户端*@authormrt**/publicclassClient{/***@paramargs*/publicstaticvoidmain(String[]args){//创建webservice服务视图WeatherServerServiceweatherServerService=newWeatherServerService();//得到服务端点WeatherServerweatherServer=weatherServerService.getWeatherServerPort();//调用webservice服务方法StringresultString=weatherServer.queryWeather(郑州);System.out.println(resultString);}}4WsdlXml标签:Service视图Binging协议体的格式Porttype服务端点Message消息体第一步:阅读wsdl,搞清楚服务视图service、协议格式binding服务端点porttype第二步:创建服务视图第三步:创建服务端点第四步:通过服务端点调用服务方法实例:调用天气查询webservice第一步:找到天气查询的webserviceWSDL第二步:根据WSDL生成客户端调用类,使用wsimport生成Wsimport–s.file:///d:\WeatherWebService.wsdl第三步:将生成的代码拷贝至工程中第四步:编写客户端调用代码packagecn.itcast.ws.client;importjava.util.List;importcn.com.webxml.ArrayOfString;importcn.com.webxml.WeatherWebService;importcn.com.webxml.WeatherWebServiceSoap;/***获取某个城市的天气*@authorThinkpad**/publicclassClient{publicstaticvoidmain(String[]args){//获取天气服务的视图WeatherWebServiceweatherWebService=newWeatherWebService();//获得服务端点WeatherWebServiceSoapweatherserver=weatherWebService.getWeatherWebServiceSoap();//通过服务端点调用webserviceStringcityName=北京;//webservice服务方法ArrayOfStringresultsArrayOfString=weatherserver.getWeatherbyCityName(cityName);ListStringresultStrings=resultsArrayOfString.getString();for(Strings:resultStrings){System.out.println(s);}}}5SoapWsimport仅支持soap1.1通过使用myeclipse监控soap协议体通过tcp/ipmoniter监视soap协议体和协议头Soap1.1实例:请求:POST/weatherHTTP/1.1Host:192.168.3.100:8980Content-Type:text/xml;charset=utf-8Content-Length:348Accept:application/soap+xml,application/dime,multipart/related,text/*User-Agent:IBMWebServicesExplorerCache-Control:no-cachePragma:no-cacheSOAPAction:Connection:closesoapenv:Envelopexmlns:soapenv=:q0=:xsd=:xsi=:Bodyq0:queryWeatherarg0閮戝窞/arg0/q0:queryWeather/soapenv:Body/soapenv:Envelope响应:HTTP/1.1200OKTransfer-encoding:chunkedContent-type:text/xml;charset=utf-85e?xmlversion=1.0?S:Envelopexmlns:S=:Body8ans2:queryWeatherResponsexmlns:ns2=闆鹃溇/return/ns2:queryWeatherResponse/S:Body/S:Envelope06jax-ws发布为web工程Jax-ws开发的webservice发布至web容器需要使用jax-wsRI(扩展实现),下载jax-wsRI最新版本,本教程使用jaxws-ri-2.2.8版本。1、创建javaweb工程2、将jaxws-ri-2.2.8下的jar拷贝至javaweb工程的lib目录3、编写服务端代码,服务端代码与使用jax-ws开发普通java工程一致4、使用wsgen生成wsdl4、在W