- 浏览: 458015 次
- 性别:
- 来自: 潘多拉
文章分类
最新评论
-
lijunwyf:
代码好像不完整,后端没串起来的感觉
Json-RPC for java首次尝试 -
jerry.chen:
我用这种方式去,貌似没啥效果,楼主!
Xfire开发的webservice客户端超时问题解决 -
漫步邃思:
这个问题常遇到,老是想不起来解决方法,记住了
resin3.1.0无法解析EL表达式问题解决 -
dampce032:
在不删掉integratedSecurity=true;的情况 ...
JDBC 连接SQLServer数据库(Failed to load the sqljdbc_auth.dll) -
kill_e680:
取CPU号和取硬盘号,在linux下可以用吗?
sigar使用:在web中应用sigar取得系统信息
本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
什么是WSDL?
● WSDL代表Web Services Description Language
● WSDL是用XML写的
● WSDL是一个XML文档
● WSDL用于描述Web Services
● WSDL也用于定位Web services
● WSDL还不是一个W3C标准
WSDL描述Web Services
WSDL代表Web Services Description Language。
WSDL是一个用XML写的XML文档。该文档描述了一个Web Service。它制定了该Web Service的位置和所提供的操作operations (或方法 methods)。
WSDL文档的结构
WSDL文档描述一个Web Service主要通过以下元素:
Element--Defines
<portType>web service执行的操作
<message>web service使用的消息
<types>web service使用的数据类型
<binding>web service使用的通信协议
WSDL文档的主要结构为
WSDL Ports
<portType>元素是最重要的WSDL元素
它描述了一个web service所能够执行的操作,和所包含的消息。
<portType>元素就相当于传统编程语言中的function library函数库(或者一个模块、一个类)
WSDL Messages
<message>元素定义了一个操作的数据元素。
每一条消息包括一个或者多个部分。这些部分相当于传统编程语言中函数的参数。
WSDL Types
<types>元素定义web service所使用的数据类型
为了保证最大的平台无关性,WSDL使用XML Schema语义来定义数据类型
WSDL Bindings
<binding>元素定义为每一个port定义消息格式(message format)和协议(protocol)
下面是一些常见的命名空间:
prefix namespace URI
wsdl http://schemas.xmlsoap.org/wsdl/
soap http://schemas.xmlsoap.org/wsdl/soap/
http http://schemas.xmlsoap.org/wsdl/http/
mime http://schemas.xmlsoap.org/wsdl/mime/
soapenc http://schemas.xmlsoap.org/soap/encoding/
soapenv http://schemas.xmlsoap.org/soap/envelope/
xsi http://www.w3.org/2000/10/XMLSchema-instance
xsd http://www.w3.org/2000/10/XMLSchema
tns (various)
下面举例说明如何编写WSDL文档:我们做一个非常简单的加法运算服务:客户端传入SOAP请求消息,包含两个加数,然后在服务器端获取这两个加数,求和,然后返回给客户端。请求消息和响应消息结构分别如下(有效负载部分,外层的SOAP封装AXIS2会自动添加上去):
1.首先需要进行定义的就是soap消息的数据类型,无疑,这里需要定义两个复杂的数据类型:SumRequestSumResponse它们分别包含有子元素First、Second和Result.另外还需要定义一种异常,这里我们定义成为SumFault,比如传入的两个加数的和超出了xsd:int的范围时,抛出该异常。(注意,很多代码生成器都会根据WSDL生成代码,将SumFault部分生成为后缀为Exception的异常类。)
2.定义数据类型的目的是为传入传出消息做准备的,传入传出消息的定义方式使用message元素来定义。我们定义三种消息:SumRequest,SumResponse以及SumFault,分别为请求消息,响应消息以及出错时的消息。
3.定义好了传入传出消息后,就要定义一个portType,该节点类型定义了主要的业务操作。4.接着将定义SOAP绑定:SumSoapBinding:为SumService端口类型所定义的操作和消息指定具体传输中所使用的消息格式和协议细节。绑定的方式为SOAP,传输方式为http,消息的格式为document。
5.定义Web服务:Web服务名为SumService。
6.本Web服务的操作为Sum操作。
7.端口为:为SumSoapBing绑定指定一个地址来定义服务访问点。
根据以上七个部分编写完成的WSDL文件如下:
服务器端和客户端的代码是根据WSDL文件编写出来的,这才是正确的过程,但是,在国内的软件开发过程,常常是先写代码,再根据代码生成WSDL文件,这是不正确的。
1. 建立工程ExampleService
2. 编写服务器端代码:
根据省的id得到省的所有电视台的id
什么是WSDL?
● WSDL代表Web Services Description Language
● WSDL是用XML写的
● WSDL是一个XML文档
● WSDL用于描述Web Services
● WSDL也用于定位Web services
● WSDL还不是一个W3C标准
WSDL描述Web Services
WSDL代表Web Services Description Language。
WSDL是一个用XML写的XML文档。该文档描述了一个Web Service。它制定了该Web Service的位置和所提供的操作operations (或方法 methods)。
WSDL文档的结构
WSDL文档描述一个Web Service主要通过以下元素:
Element--Defines
<portType>web service执行的操作
<message>web service使用的消息
<types>web service使用的数据类型
<binding>web service使用的通信协议
WSDL文档的主要结构为
<definitions> <types> definitions of types...... </types> <message> definitions of a message... </message> <portType> definitions of a port... </portType> <binding> definitions of a binding... </binding> </definations>
WSDL Ports
<portType>元素是最重要的WSDL元素
它描述了一个web service所能够执行的操作,和所包含的消息。
<portType>元素就相当于传统编程语言中的function library函数库(或者一个模块、一个类)
WSDL Messages
<message>元素定义了一个操作的数据元素。
每一条消息包括一个或者多个部分。这些部分相当于传统编程语言中函数的参数。
WSDL Types
<types>元素定义web service所使用的数据类型
为了保证最大的平台无关性,WSDL使用XML Schema语义来定义数据类型
WSDL Bindings
<binding>元素定义为每一个port定义消息格式(message format)和协议(protocol)
下面是一些常见的命名空间:
prefix namespace URI
wsdl http://schemas.xmlsoap.org/wsdl/
soap http://schemas.xmlsoap.org/wsdl/soap/
http http://schemas.xmlsoap.org/wsdl/http/
mime http://schemas.xmlsoap.org/wsdl/mime/
soapenc http://schemas.xmlsoap.org/soap/encoding/
soapenv http://schemas.xmlsoap.org/soap/envelope/
xsi http://www.w3.org/2000/10/XMLSchema-instance
xsd http://www.w3.org/2000/10/XMLSchema
tns (various)
下面举例说明如何编写WSDL文档:我们做一个非常简单的加法运算服务:客户端传入SOAP请求消息,包含两个加数,然后在服务器端获取这两个加数,求和,然后返回给客户端。请求消息和响应消息结构分别如下(有效负载部分,外层的SOAP封装AXIS2会自动添加上去):
request: <SumRequest> <First>15</First> <Second>16</Second> </SumRequest> response: <SumResponse> <Result>31</Result> </SumResponse>
1.首先需要进行定义的就是soap消息的数据类型,无疑,这里需要定义两个复杂的数据类型:SumRequestSumResponse它们分别包含有子元素First、Second和Result.另外还需要定义一种异常,这里我们定义成为SumFault,比如传入的两个加数的和超出了xsd:int的范围时,抛出该异常。(注意,很多代码生成器都会根据WSDL生成代码,将SumFault部分生成为后缀为Exception的异常类。)
2.定义数据类型的目的是为传入传出消息做准备的,传入传出消息的定义方式使用message元素来定义。我们定义三种消息:SumRequest,SumResponse以及SumFault,分别为请求消息,响应消息以及出错时的消息。
3.定义好了传入传出消息后,就要定义一个portType,该节点类型定义了主要的业务操作。4.接着将定义SOAP绑定:SumSoapBinding:为SumService端口类型所定义的操作和消息指定具体传输中所使用的消息格式和协议细节。绑定的方式为SOAP,传输方式为http,消息的格式为document。
5.定义Web服务:Web服务名为SumService。
6.本Web服务的操作为Sum操作。
7.端口为:为SumSoapBing绑定指定一个地址来定义服务访问点。
根据以上七个部分编写完成的WSDL文件如下:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.zzl.org/Sum" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zzl.org/Sum"> <wsdl:documentation> The WSDL file of SumService. </wsdl:documentation> <wsdl:types> <wsdl:documentation> Data types that are used for request and response messages. </wsdl:documentation> <xsd:schema targetNamespace="http://www.zzl.org/Sum"> <xsd:element name="SumRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:int" /> <xsd:element name="Second" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SumResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="Result" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SumFault"> <xsd:complexType> <xsd:sequence> <xsd:element name="Code" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="SumRequest"> <wsdl:documentation> The data that will be transmitted to the service. </wsdl:documentation> <wsdl:part element="tns:SumRequest" name="request" /> </wsdl:message> <wsdl:message name="SumResponse"> <wsdl:documentation> The data that will be returned to the client. </wsdl:documentation> <wsdl:part element="tns:SumResponse" name="response" /> </wsdl:message> <wsdl:message name="SumFault"> <wsdl:documentation> The fault that will be thrown when fault occurs. </wsdl:documentation> <wsdl:part name="axisFault" element="tns:SumFault" /> </wsdl:message> <wsdl:portType name="SumService"> <wsdl:documentation> The SumService contains the business operation. </wsdl:documentation> <wsdl:operation name="RevokeCert"> <wsdl:documentation> The operation that do the business work. </wsdl:documentation> <wsdl:input message="tns:SumRequest" /> <wsdl:output message="tns:SumResponse" /> <wsdl:fault name="fault" message="tns:SumFault" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SumSoapBinding" type="tns:SumService"> <wsdl:documentation> The SumSoapBinding defines the SOAP message format and protocol details for Sum operation and messages defined by a RevokeService portType. </wsdl:documentation> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Sum"> <soap:operation soapAction="urn:Sum" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> <wsdl:fault name="fault"> <soap:fault use="literal" name="fault" /> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="SumService"> <wsdl:documentation> SumService provides the service of summing. </wsdl:documentation> <wsdl:port binding="tns:SumSoapBinding" name="SumSoapBinding"> <wsdl:documentation> The port defines the endpoint by specifying a soap address for SumSoapBinding. </wsdl:documentation> <soap:address location="http://www.zzl.org/ExampleService/services/SumService" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
服务器端和客户端的代码是根据WSDL文件编写出来的,这才是正确的过程,但是,在国内的软件开发过程,常常是先写代码,再根据代码生成WSDL文件,这是不正确的。
1. 建立工程ExampleService
2. 编写服务器端代码:
SumService.java package org.zzl.service; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; /** * The Web Service class SumService which implement add two number and return * the result. * * @author zhangzhongl@gmail.com * @version 0.7 */ public class SumService { /** * The request soap message object. */ private OMElement requestSoap = null; /** * First addend. */ private static final String FIRST = "First"; /** * Second addend. */ private static final String SECOND = "Second"; /** * Sum Response element. */ private static final String SUM_RESPONSE = "SumResponse"; /** * Result element. */ private static final String SUM = "Result"; public OMElement Sum(OMElement soap) throws AxisFault { requestSoap = soap; OMElement first= requestSoap.getFirstChildWithName(new QName(FIRST)); OMElement second = requestSoap.getFirstChildWithName(new QName(SECOND)); int sum = Integer.parseInt(first.getText()) + Integer.parseInt(second.getText()); return getResponse(sum); } /** * Get the SOAP response message. * * @param sum * The adding result. * @return The SOAP response message. */ private OMElement getResponse(int sum) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ōmNs = factory.createOMNamespace("", ""); OMElement response = factory.createOMElement(SUM_RESPONSE, omNs); OMElement sumElement = factory.createOMElement(SUM, omNs); sumElement.setText(String.valueOf(sum)); response.addChild(sumElement); return response; } } 编写客户端代码: TestSumService.java package org.zzl.service.test; import java.io.FileInputStream;import java.io.FileNotFoundException; import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TesSumService{ private static EndpointReference targetEPR = new EndpointReference( "http://localhost/axis2/services/SumService"); public static void main(String[] args) throws FileNotFoundException, FactoryConfigurationError, XMLStreamException {OMElement requestSoapMessage = getSoapRequestMessage("data/request.xml"); Options options = new Options(); options.setAction("urn:Sum"); options.setTo(targetEPR); ServiceClient sender = null; try { sender = new ServiceClient(); sender.setOptions(options); System.out.println(sender.sendReceive(requestSoapMessage) .toStringWithConsume()); } catch (AxisFault e) { System.out.println(e.getMessage()); } } public static OMElement getSoapRequestMessage(String filePath) throws FileNotFoundException, XMLStreamException, FactoryConfigurationError { XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new FileInputStream(filePath)); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement requestMessage = builder.getDocumentElement(); return requestMessage; } }
根据省的id得到省的所有电视台的id
http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx/getTVstationDataSet?theAreaID=13 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
发表评论
-
eclipse
2012-01-19 10:19 942eclipse操作常识:eclipse进行项目开发时往往会遇 ... -
sigar使用:在web中应用sigar取得系统信息
2012-01-18 14:19 3256[转] import java.io.IOExce ... -
解决struts1.x表单重复提交问题
2012-01-17 10:40 2339每次填写完表单后单击提交后,struts中action执行相关 ... -
在树初始化时,加一个load图片
2012-01-16 09:45 1823做个浮动图层放在 zTree 上面, 然后利用 asnycS ... -
利用JS做到隐藏div和显示div
2012-01-16 09:05 108725div的visibility可以控制div的显示和隐藏,但是隐 ... -
虚拟机使用NAT不能上网
2012-01-05 08:41 2980虚拟机上网问题 许多虚拟机的初学者,都会询问这样一个问题:在 ... -
[转]用eclipse pdt 配置php开发,调试环境
2011-12-30 16:15 1479本文档是windows操作系统下php开发环境的配置。 1 ... -
RESIN 连接池数据库密码加密方法
2011-12-28 15:34 1600[转] Resin 连接池写在resin.conf里 ... -
好的技术博客
2011-12-28 15:18 849http://www.findjar.com/index.x ... -
利用jawin完成调用window中dll的调用
2011-12-28 15:14 2085[转] Java/Win32互动项目( Jawin)是 ... -
WebService的测试
2011-12-20 09:26 1453在浏览器中输入地址:http://localhost ... -
我的SQLSERVER 分页存储过程
2011-12-13 08:54 3163/** * procedure name : kk_f ... -
Myeclipse中将Web项目发布时重命名
2011-12-12 09:58 1138选中项目->反键->Properties ... -
Sql xtype 类型
2011-11-30 09:39 1221sysobjects 表 在数据库内创建的每个对象(约束、默认 ... -
使用SQL语句清空数据库所有表的数据[转]
2011-11-30 09:14 774近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的 ... -
js实现焦点进入文本框内关闭输入法
2011-11-30 08:56 3541要用到的东西: imeMode:xxx 有四个参数 act ... -
数据库设计三大范式应用实例剖析[转]
2011-11-29 17:25 770数据库的设计范式是数据库设计所需要满足的规范,满足这些规范的数 ... -
Ajax.Request方法
2011-11-29 13:47 1255Prototype 的 Ajax.Request 对象绑定了浏 ... -
临时表(Template Table)[转]
2011-11-25 17:41 15651、MS SQLSERVER SQL ... -
测试sql语句性能
2011-11-25 17:22 1016[size=medium]有时候我们经常为我们的sql语句执行 ...
相关推荐
本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
wsdl4j 解析wsdl 文件wsdl4j 解析wsdl 文件wsdl4j 解析wsdl 文件
把wsdl文件转换成java类 使用wsdl2Java工具
wsdl文件结构 元素的详细说明及其使用
根据WSDL文件生成客户端代码与创建客户端工程 注意小细节:在根据对方提供的webservice地址,先将对方webservice的wsdl文件描述文件保存到本地时,格式先保存为xml,后将其后缀名为.wsdl的文件 否则报:...
WebService的WSDL文件生成Java代码工具
Eclipse根据wsdl文件逆向生成webservice客户端项目源码,包括wsdl文件,测试示例,测试webservice代码,包含备注信息
最新联通VAC接口WSDL文件 VacSyncService_SPClient_20091212.wsdl.xml
在myeclipse下的一个web程序,把wsdl文件转化成java代码
onvif开发所有wsdl文件(修改过),下载后就可以直接用wsimport生成java本地代码
最新的wsdl文件。通过这些文件可以生成最新的完整的onvif协议开发代码。
一个wsdl文件,用axis2解析,一个wsdl文件,用axis2解析,
onvif wsdl文件,并且带了gsoap生成脚本
通过wsdl文件就可以通过客户端调用服务端的接口,可以使用Webservice的服务了
onvif的wsdl文件2016
WSDL文件结构详解 WSDL文件结构详解 WSDL文件结构详解 WSDL文件结构详解
详细描述了wsdl文件的一些属性及其意义
gsoap移植之wsdl文件,IPCAM,网络摄像头
对webservic中 wsdl文件进行了详细的分析和介绍