版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、WebService培训,Web service 就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。我们把调用这个Web service 的应用程序叫做客户。 关于客户端与服务器的通信问题,一个完美的解决方法是使用HTTP协议来通信。这是因为任何运行Web浏览器的机器都在使用HTTP协议。同时,当前许多防火墙也配置为只允许HTTP连接。,WebService概念与用途,1.XML和XSD可扩展的标记语言(XML)是Web service平台中表示数据的基本格式。除了易于建立和易于分析外,XML主要的优点在于它既是平台无
2、关的,又是厂商无关的。无关性是比技术优越性更重要的:软件厂商是不会选择一个由竞争对手所发明的技术的。 XML解决了数据表示的问题,但它没有定义一套标准的数据类型,更没有说怎么去扩展这套数据类型。例如,整形数到底代表什么?16位,32位,还是64位?这些细节对实现互操作性都是很重要的。W3C制定的XML Schema(XSD)就是专门解决这个问题的一套标准。它定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型。Web service平台就是用XSD来作为其数据类型系统的。当你用某种语言(如VB.NET或C#)来构造一个Web service时,为了符合Web service标准,所有你
3、使用的数据类型都必须被转换为XSD类型。你用的工具可能已经自动帮你完成了这个转换,但你很可能会根据你的需要修改一下转换过程。,2.SOAPSOAP即简单对象访问协议(SimpleObjectAccessProtocol),它是用于交换XML编码信息的轻量级协议。它有三个主要方面:XML-envelope为描述信息内容和如何处理内容定义了框架,将程序对象编码成为XML对象的规则,执行远程过程调用(RPC)的约定。SOAP可以运行在任何其他传输协议上。例如,你可以使用SMTP,即因特网电子邮件协议来传递SOAP消息,这可是很有诱惑力的。在传输层之间的头是不同的,但XML有效负载保持相同。,3.WS
4、DL你会怎样向别人介绍你的Web service有什么功能,以及每个函数调用时的参数呢?你可能会自己写一套文档,你甚至可能会口头上告诉需要使用你的Web service的人。这些非正式的方法至少都有一个严重的问题:当程序员坐到电脑前,想要使用你的Web service的时候,他们的工具(如Visual Studio)无法给他们提供任何帮助,因为这些工具根本就不了解你的Web service。解决方法是:用机器能阅读的方式提供一个正式的描述文档。Web service描述语言(WSDL)就是这样一个基于XML的语言,用于描述Web service及其函数、参数和返回值。因为是基于XML的,所以W
5、SDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。一些最新的开发工具既能根据你的Web service生成WSDL文档,又能导入WSDL文档,生成调用相应Web service的代码。,常用开源Web Service框架 Apache Axis和Axis2 Codehaus Xfire Apache CXF,Java语言的 Web Service,axis 主页 /axis/ 依赖类库 actication-1.1.jar axis.jar axis-ant.jar commons-discovery-0.2.jar commons-loggi
6、ng-1.0.4.jar jaxrpc.jar log4j-1.2.8.jar mail-1.4.jar saaj.jar wsdl4j-1.5.1.jar xercesImpl.jar xml-apis.jar xmlsec-1.3.0.jar,Axis开发,发布目录结构 主要配置文件 1 .web.xml 添加AxisServlet,用于发布webservice 2. server-config.wsdd Webservice参数文件,如暴露的方法,Web.xml AxisServlet org.apache.axis.transport.http.AxisServlet AxisServ
7、let /services/* SOAPMonitorService /SOAPMonitor AxisServlet /servlet/AxisServlet AxisServlet *.jws AxisServlet /services/* ,server-config.wsdd , , http:/service.be- ,Service Styles RPC, Document, Wrapped, and Message provider=“java:MSG” provider=java:RPC”,你可以传递自定义的JAVA类,但那样你必须自己创建专门的XML序列化器和反序列化器。而对
8、JavaBean,AXIS提供了现成的序列化器 自定义对象序列化 详细文档参阅 /axis/java/user-guide.html 上述服务发布的wsdl地址可参见 65/beapp/services/BeappWSServerV1?wsdl,服务端 public class HelloWorld public String sayHello() return hello world!; server-config.wsdd ,Hello world,客户端: import org.apache.axis.client.
9、Call; import org.apache.axis.client.Service; import space.QName; public class TestClient public static void main(String args) try String endpoint = http:/localhost:8080/axis/services/HelloWorld; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEnd
10、pointAddress(new .URL(endpoint); call.setOperationName(new QName(sayHello); String ret = (String) call.invoke(new Object); System.out.println( got + ret + ); catch (Exception e) System.err.println(e.toString(); ,服务端,传递javabean示例,server-config.wsdd ,客户端 客户端也需要Order.java import .*; import javax.xml.na
11、mespace.*; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.ser.BeanSerializerFactory; import org.apache.axis.encoding.ser.BeanDeserializerFactory; public class OrderClient public static void main(String ar
12、gs) throws Exception Order order = new Order(); / JavaBean order.setId(1); String endpoint = http:/localhost:8080/axis/services/order; / 服务所在位置 Service service = new Service(); Call call = (Call) service.createCall(); / 注册JavaBean,注意和server-config.wsdd中的配置代码比较 QName qn = new QName(urn:BeanService, o
13、rder); call.registerTypeMapping(Order.class, qn, new BeanSerializerFactory( Order.class, qn), new BeanDeserializerFactory(Order.class, qn); String name = no!; try call.setTargetEndpointAddress(new URL(endpoint); / 调用的服务器端方法 call.setOperationName(new QName(order, returnOrder); / 设定传入的参数,这里qn即Order.cl
14、ass call.addParameter(arg1, qn, ParameterMode.IN); / 设定返回的参数是Order.class call.setReturnType(qn, Order.class); Order result = (Order) call.invoke(new Object order ); if (result != null) name = result.getName(); catch (Exception ex) /System.err.println(ex); System.out.println(name); ,编写客户端程序可使用axis提供的
15、ant工具生成代码 工程确保有axis-ant.jar文件,否则报错 编写ant文件(build.xml) ,生成后的工程结构如下,axis中有两种方法实现session,一种是依靠HTTP和HTTP cookies,另一种是用SOAP headers。 1.依靠cookie 服务器端SessionService.java import org.apache.axis.MessageContext; import org.apache.axis.session.Session; import mons.logging.Log; import mons.logging.LogFactory; p
16、ublic class SessionService Log log = LogFactory.getLog(SessionService.class); public String echo(String in) MessageContext mc = MessageContext.getCurrentContext(); Session session = mc.getSession(); (session : + session); String name = (String)session.get(name); session.set(name, name + - in
17、put : + in); (in + - name : + name + ); return in; ,在axis中使用session,deployment xmlns=/axis/wsdd/ xmlns:java=/axis/wsdd/providers/java axis会在响应信息中添加对cookie的操作 Set-Cookie: JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9; Path=/axis 客户端SessionClient.java publ
18、ic class SessionClient public static void main(String args) throws Exception Log log = LogFactory.getLog(SessionClient.class); String endpoint = http:/localhost:8080/axis/services/SessionService; Service service = new Service(); service.setMaintainSession(true);/设置为保持会话 Call call = (Call) service.cr
19、eateCall(); call.setTargetEndpointAddress(new .URL(endpoint); call.setOperationName(echo); String res = (String) call.invoke(new Objectlingo); System.out.println(res); client会在请求中添加cookie的信息 Cookie: JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9,public class FileService Log log = LogFactory.getLog(File
20、Service.class); public static String Repository = ./files/; public String putFile(DataHandler dh, String name) if (name = null) name = test.tmp; (name : + name); try File dir = new File(Repository); if (!dir.exists() dir.mkdir(); (makedir : dir : + dir); InputStream input = dh.getInp
21、utStream(); FileOutputStream fos = new FileOutputStream(new File(dir, name); byte buffer = new byte1024 * 4; int n = 0; while (n = input.read(buffer) != -1) fos.write(buffer, 0, n); /(buffer); input.close(); fos.close(); catch (IOException e) e.printStackTrace(); return name + send OK; ,使用Da
22、taHandler实现文件上传与下载,public DataHandler getFile(String name) (name : + name); File dir = new File(Repository); if (!dir.exists() dir.mkdir(); if(name != null ,server-config.wsdd ,客户端,FileClient.java import java.io.File; import space.QName; import javax.xml.rpc.ParameterMode; impo
23、rt .URL; import javax.activation.DataHandler; import javax.activation.FileDataSource; import org.apache.axis.attachments.AttachmentPart; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.encoding.ser.*; import o
24、rg.apache.axis.soap.SOAP11Constants; import mons.logging.Log; import mons.logging.LogFactory; public class FileClient Log log = LogFactory.getLog(FileClient.class); String endPoint = http:/localhost:8080/axis/services/FileService; QName qNameAttachment = new QName(FileService, DataHandler); Call cal
25、l; public FileClient() init(); void init() try Service service = new Service(); call = (Call) service.createCall(); call.setTargetEndpointAddress(new URL(endPoint); call.registerTypeMapping(DataHandler.class, qNameAttachment, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.c
26、lass); catch (Exception ex) log.fatal(ex); System.exit(1); ,void testPutFile() throws Exception (start); init(); String fileName = build.xml; DataHandler dataHandler = new DataHandler(new FileDataSource(fileName); call.setOperationName(new QName(endPoint, putFile);/ 指定方法的命名空间 call.addParameter(s1, qNameAttachment, ParameterMode.IN); call.addParameter(s2, XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论