WebService远程调用技术--学习01
来源:互联网 发布:一直播怎么放淘宝链接 编辑:程序博客网 时间:2024/06/08 07:37
WebService就是一种远程调用技术,它的作用就是从远程系统中获取业务数据。
什么是WebService
远程调用数据定义:是系统和系统之间的调用
WebService的原理图:WebService是使用Http发送SOAP协议的数据的一种远程调用技术,需要开发服务端和客户端,客户端开发需要阅读服务端的使用说明书(WSDL)
WebService的应用场景
软件集成和复用:
适用场景:1)发布一个服务(对内/对外),不考虑客户端类型,不考考虑性能,建议使用WebService。2)服务端已经确定使用webservice,客户端不能选择,必须使用webservice。
不适用场景:1)考虑性能时,不建议使用webservice。2)同构程序下不建议使用webservice,比如java用RMI,不需要翻译成XML的数据。WebService的三要素
1)WSDL
定义:web服务描述语言,是webservice服务端使用说明书,说明服务端接口、方法、参数、返回值,WSDL是随服务发布成功,自动生成,无需编写。
文档结构:
-service:服务视图,webservice的服务节点,它包括了服务端点
-binding:为每个服务端点定义消息格式和协议细节
-portType:服务端点,描述webservice可被执行的操作方法,以及相关的消息,通过bingding指向portType
-message:定义一个操作(方法)的数据参数(可有多个参数)
-types:定义webservice使用的全部数据类型
阅读方式:从下往上
2)SOAP
定义:简单对象访问协议,是使用http发送的XML格式数据,它可以跨平台,跨防火墙,SOAP不是webservice的专有协议,SOAP=http+xml。
协议格式:1)必需有Envelope元素,此元素将整个XML文档标识为一条SOAP消息。2)可选的Header元素,包含头部信息。3)必需有Body元素,包含所有的调用和响应信息。
-*-SOAP1.1###请求POST /weather HTTP/1.1Accept: text/xml, multipart/relatedContent-Type: text/xml; charset=utf-8SOAPAction: "http://ws.jaxws.ws.cn/WeatherInterfaceImpl/queryWeatherRequest"User-Agent: JAX-WS RI 2.2.4-b01Host: 127.0.0.1:54321Connection: keep-aliveContent-Length: 214<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:queryWeather xmlns:ns2="http://ws.jaxws.ws.cn/"><arg0>北京</arg0></ns2:queryWeather></S:Body></S:Envelope>###响应HTTP/1.1 200 OKTransfer-encoding: chunkedContent-type: text/xml; charset=utf-8Date: Thu, 26 Nov 2015 03:14:29 GMT<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:queryWeatherResponse xmlns:ns2="http://ws.jaxws.ws.cn/"><return>晴</return></ns2:queryWeatherResponse></S:Body></S:Envelope>
-*-SOAP1.2###请求POST /weather HTTP/1.1Accept: application/soap+xml, multipart/relatedContent-Type: application/soap+xml; charset=utf-8;action="http://ws.jaxws.ws.cn/WeatherInterfaceImpl/queryWeatherRequest"User-Agent: JAX-WS RI 2.2.4-b01Host: 127.0.0.1:54321Connection: keep-aliveContent-Length: 212<?xml version="1.0" ?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Body><ns2:queryWeather xmlns:ns2="http://ws.jaxws.ws.cn/"><arg0>北京</arg0></ns2:queryWeather></S:Body></S:Envelope>###响应HTTP/1.1 200 OKTransfer-encoding: chunkedContent-type: application/soap+xml; charset=utf-8Date: Thu, 26 Nov 2015 03:25:24 GMT<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Body><ns2:queryWeatherResponse xmlns:ns2="http://ws.jaxws.ws.cn/"><return>晴</return></ns2:queryWeatherResponse></S:Body></S:Envelope>
SOAP1.1和SOAP1.2的区别:
1)相同点:请求发送方式相同,都是POST;协议内容相同,都有Envelope和Body标签。
2)不同点:数据格式不同,content-type不同,soap1.1(text/xml;charset=utf-8),soap1.2(application/soap+xml;charset=utf-8);命名空间不同,soap1.1(http://schemas.xmlsoap.org/soap/envelope/),soap1.2(http://www.w3.org/2003/05/soap-envelope)
3)UUID
是一种目录服务,企业可以使用它对webservice进行注册和搜索。UUID英文为“Universal Description,Discovery and Integration”,可译为“通用描述、发现与集成服务”。UUID并不想WSDL和SOAP一样深入忍心,应为很多时候,使用者知道web服务的未知(通常位于公司企业的内部网中)
4.WebService的入门程序
服务端第一步:创建SEI(Service Endpoint Interface)接口 public interface WeatherInterface { String queryWeather(String cityName); }第二步:创建SEI实现类,要在类上加入@WebService @WebService//@WebService表示该类是一个服务类,需要发布其中的public方法 public class WeatherInterfaceImpl implements WeatherInterface { @Override public String queryWeather(String cityName) { System.out.println("from client。。。" + cityName); String weather = "晴天"; return weather; } }第三步:发布服务,Endpoint的publish方法,2两个参数:1.服务地址;2.实现类实例 public class WeatherService { public static void main(String[] args){ //Endpoint发布服务 //参数解析:1.address-服务地址 2.implementor-实现类 Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl()); } }第四步:测试服务是否发布成功,通过阅读使用说明书,确定服务接口、方法、参数、返回值存在,说明服务发布成功。 WSDL地址:服务地址+”?wsdl” WSDL阅读方式,从下往上,servvice->binding->portType->其中有接口、方法、参数和返回值
客户端第一步:使用wsimport生成客户端代码wsimport -p cn.ws.jaxws.ws -s . http://127.0.0.1:12345/weather?wsdl 第二步:根据使用说明书,使用客户端调用服务端 1.创建服务视图,视图是从service的name属性获取 2.获取服务实现类,从portType的name属性获取 3.调用查询方法,从portType下的operation标签的name属性获取 public class WeatherClient { public static void main(String[] args){ //创建服务视图 WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService(); //获取服务实现类 WeatherInterfaceImpl weatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class); //调用查询方法,打印 String queryWeather = weatherInterfaceImpl.queryWeather("上海"); System.out.println(queryWeather); } }
WebService的优缺点优点:1.发送方式是采用http的post发送,http的默认端口是80,防火墙默认不拦截80,所以跨防火墙。2.采用XML格式封装数据,XML是跨平台的,所以webservice也可以跨平台。3.webservice支持面向对象缺点:采用XML格式封装数据,所有在传输过程中,要传输额外的标签,随着SOAP协议的不断完善,标签越来越大,导致webservice性能大降。
5.WebService的四种客户端调用方式
公网服务地址:http://www.webxml.com.cn/zh_cn/index.aspx1.生成客户端调用方式Wsimport命令介绍 Wsimport就是jdk提供的的一个工具,他作用就是根据WSDL地址生成客户端代码 Wsimport位置JAVA_HOME/bin Wsimport常用的参数: -s,生成java文件的 -d,生成class文件的,默认的参数 -p,指定包名的,如果不加该参数,默认包名就是wsdl文档中的命名空间的倒序 Wsimport仅支持SOAP1.1客户端的生成/** *1.生成客户端调用方式 * 1).wsimport生成客户端代码wsimport -p cn.ws.jaxws.mobile.client -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl * 2).阅读使用说明书,使用生成客户端代码调用服务端 * *公网手机号查询客户端 */public class MobileClient { public static void main(String[] args) { //创建服务视图 MobileCodeWS mobileCodeWS = new MobileCodeWS(); //获取服务实现类 MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class); //调用查询方法 String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("13888886666", ""); System.out.println(mobileCodeInfo); ArrayOfString arrayOfString = mobileCodeWSSoap.getDatabaseInfo(); List<String> list = arrayOfString.getString(); for (String str : list) { System.out.println(str); } }}特点:该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。2.service编程调用方式/** *2.Service编程调用方式 */public class ServiceCient { public static void main(String[] args) throws Exception { //创建WSDL的URL,注意不是服务地址 URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); /** * 创建服务名称 * 1.namespaceURL - 命名空间地址 * 2.localPort - 服务视图名 */ QName qName = new QName("http://WebXml.com.cn/", "MobileCodeWS"); /** * 创建服务视图 * 参数解析: * 1.wsdlDocumentLocation - wsdl地址 * 2.serviceName - 服务名称 */ Service service = Service.create(url, qName); //获取服务实现类 MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class); //调用查询方法 String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("13888886666", ""); System.out.println(mobileCodeInfo); ArrayOfString arrayOfString = mobileCodeWSSoap.getDatabaseInfo(); List<String> list = arrayOfString.getString(); for (String str : list) { System.out.println(str); } }}特点:该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式3.HttpURLConnection调用方式/** *3.HttpURLConnection调用方式 */public class HttpClient { public static void main(String[] args) throws Exception { //1.创建服务地址,不是wsdl地址 URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); //2.打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /** * 3.设置参数 * 3.1发送方式设置:POST必须大写 * 3.2设置数据格式:content-type * 3.3必须设置输入输出,因为默认新创建的connection没有读写权限 */ connection.setRequestMethod("POST"); connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); connection.setDoInput(true); connection.setDoOutput(true); //4.组织SOAP数据,发送请求 String soapXML = getXML("13888886666"); OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes()); //5.接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while (null != (temp = br.readLine())) { sb.append(temp); } System.out.println(sb.toString()); is.close(); isr.close(); br.close(); } os.close(); } /** * <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap:Body></soap:Envelope> * @param phoneNum * @return *//* <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>13888886666:河南 郑州 河南移动全球通卡 </getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap:Body> </soap:Envelope>*/ private static String getXML(String phoneNum) { String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" +"<mobileCode>"+phoneNum+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; return soapXML; }}4.Ajax调用方式<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript"> function queryMobile(){ //创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); //打开连接 xhr.open("post","http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx",true); //设置数据类型 xhr.setRequestHeader("content-type","text/xml;charset=utf-8"); //设置回调函数 xhr.onreadystatechange=function(){ //判断是否发送成功和判断服务端是否响应成功 if(4 == xhr.readyState && 200 == xhr.status){ alert(xhr.responseText); } } //组织SOAP协议数据 var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" +"<mobileCode>"+document.getElementById("phoneNum").value+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; alert(soapXML); //发送数据 xhr.send(soapXML); } </script></head><body> 手机号查询:<input type="text" id="phoneNum"/> <input type="button" value="查询" onclick="javascript:queryMobile();"/></body></html>
6.用注解修改WSDL内容
@WebService( targetNamespace="http://service.cn.ws.assess", //指定命名空间 name="WeatherWSSoap", //portType的名称 portName="WeatherWSSoapPort", //port的名称 serviceName="WeatherWS" //服务名称 )//@WebService表示该类是一个服务类,需要发布其中的public方法public class WeatherInterfaceImpl implements WeatherInterface { @WebMethod( operationName="getWeather",//方法名 exclude=false//设置为true表示此方法不是webservice方法,反之则表示webservice方法,默认是false ) @Override public @WebResult(name="result")String queryWeather(@WebParam(name="cityName")String cityName) { System.out.println("from client。。。" + cityName); String weather = "晴天"; return weather; }}WebService的注解都位于javax.jws包下:@WebService-定义服务,在public class上边 targetNamespace:指定命名空间 name:portType的名称 portName:port的名称 serviceName:服务名称 endpointInterface:SEI接口地址,如果一个服务类实现了多个接口,只需要发布一个接口的方法,可通过此注解指定要发布服务的接口。@WebMethod-定义方法,在公开方法上边 operationName:方法名 exclude:设置为true表示此方法不是webservice方法,反之则表示webservice方法,默认是false@WebResult-定义返回值,在方法返回值前边 name:返回结果值的名称@WebParam-定义参数,在方法参数前边 name:指定参数的名称作用:通过注解,可以更加形像的描述Web服务。对自动生成的wsdl文档进行修改,为使用者提供一个更加清晰的wsdl文档。当修改了WebService注解之后,会影响客户端生成的代码。调用的方法名和参数名也发生了变化
- WebService远程调用技术--学习01
- WebService远程调用技术--学习02
- WebService远程调用技术
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪之(Webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 浅谈WebService跨平台远程调用技术
- webService远程调用技术二()
- 远程调用WebService
- WebService的远程调用
- 远程调用webservice
- c++远程调用webservice
- webservice远程调用详解
- axis2远程调用webservice
- pom.xml
- linux下查看系统配置参数
- 关于Linux下,文件传输与ftp以及Tomcat配置等
- 实验一 线性表的应用(2学时)
- 用h5和原生js做的漂亮的时钟
- WebService远程调用技术--学习01
- Git命令查看、创建、切换、删除分支
- Gradle 添加 Jacoco 分析单元测试覆盖率工具
- Slim 框架学习,第七天 _Container(四)
- C/C++ 实现单类模式
- 创建profile 限定用户最大连接数目
- css-字体,颜色和图片
- POJ-1273 Drainage Ditches(最大流入门)
- TreeSet以及存储自定义类重写compareTo()续