webservice初识

      最近一直 在使用公司的框架开发webservice服务接口,于是决定好好了解下webService。

      关于webservice的定义,网上一搜一大堆,但都大同小异,个人感觉不好理解。根据自己开发的经验,个人认为webservice就是通过网络暴露程序API,供其他应用调用实现RPC。所谓通过网络暴露,说通俗点就是就好比一般的web应用中浏览器向后台发送一个指定的交易请求,服务器会根据请求内容调用相关方法处理。不同的是,一般web应用客户端与服务端使用一种语言(比如java中jsp与servlet),而webservice能够做到跨语言,跨平台调用。

      完成webservice最重要的两个元素是wsdl与soap。

    wsdl的全称是web service Description Language,是一种基于XML格式的关于web服务的描述语言。其主要目的在于webservice的提供者将自己的web服务的所有  相关内容,如所提供的服务的传输方式,服务方法接口,接口参数,服务路径等,生成相应的完全文档,发布给使用者。使用者可以通过这个wsdl文档,创建相应 的SOAP请求消息,通过HTTP传递给webservice提供者;web服务在完成服务请求后,将SOAP返回消息传回请求者,服务请求者再根据wsdl文档将SOAP返回消息解析成自己能够理解的内容。但是,wsdl 并不是完全为传输soap消息而出现的,它也可以用于描述基于其他协议的服务。如:HTTP-GET/POST 或者是SMTP等。下面是一个helloworld的例子:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"
	name="HelloWorldService">
	<types>
		<xs:schema xmlns:tns="http://example.helloworld.com/"
			xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
			targetNamespace="http://example.helloworld.com/">
			<xs:element name="sayHello">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="arg0" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="sayHelloResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="return" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</types>
	<message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>
	<portType name="HelloWorld">
		<operation name="sayHello">
			<input message="tns:sayHello"></input>
			<output message="tns:sayHelloResponse"></output>
		</operation>
	</portType>
	<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
	<service name="HelloWorldService">
		<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
			<soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>
		</port>
	</service>
</definitions>

   soap是web service的标准通信协议,soap为simple object access protocoll的缩写,简单对象访问协议. 它是一种标准化的传输消息的XML消息格式。

   调用上面hellowolrd.wsdl生成客户端后调用服务发送与接收的soap报文如下:

   output:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Body>
		<ns2:sayHello xmlns:ns2="http://example.helloworld.com/">
			<arg0>Justin</arg0>
		</ns2:sayHello>
	</soap:Body>
</soap:Envelope>

input:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns2:sayHelloResponse xmlns:ns2="http://example.helloworld.com/">
			<return>Justin Say Hello To The World !</return>
		</ns2:sayHelloResponse>
	</S:Body>
</S:Envelope>

   因此,可以看出webservice之所以能够做到跨语言与平台,是因为其是基于xml+http的方式传递数据。所谓xml就是常说的soap。可以看出,与一般http请求不同的地方是,传输 的内容是一个xml,这个xml即所谓的soap报文。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值