webservice是什么?基于web的服务:服务器端整出一些资源让客户端应用访问(获取数据)一个跨平台、跨语言的规范(抽象)多个跨平台、跨平台的应用间通信整合的方案(实际) 为什么要用webservicewebservice能解决跨平台调用webservice能解决跨语言调用webservice能解决远程调用 什么时候用webservice同一家公司的新旧应用之间的通信(语言不同)不同公司的应用之间(语言不同)一些提供数据的内容就会应用:天气预报、股票行情等 webservice的几个重要的术语wsdl:webservicedefinitionlanguage(webservice定义语言) 对应一种类型的文件。wsdl 定义了webservice的服务器端与客户端应用交互传递请求和响应数据的格式和方式 一个webservice对应一个唯一的wsdl文档soap:simpleobjectaccessprotocol(简单对象访问协议) 一种简单的、基于http和xml的协议,用于在web上交换结构化的数据 soap消息:请求消息和响应消息 httpxml片段SEI:webserviceendpointinterface(webservice终端接口) webservice服务器端用来处理请求的接口cxf:celtixxfire 一个apache的用于开发webservice服务器端和客户端的框架 用jdk开发webservice开发服务器端 webservice编码:WebService(SEI的实现类);WebMethod(SEI中的所有方法) 发布webservice Endpoint(终端发布webservice) 示例代码SEI接口WebServicepublicinterfaceWebSEI{WebMethodpublicStringsayHello(Stringname);}SEI的实现类WebServicepublicclassWebSEIImplimplementsWebSEI{OverridepublicStringsayHello(Stringname){System。out。println(serversayhelloname);returnH}}发布webservicepublicclassWebEndpoint{publicstaticvoidmain(String〔〕args){参数说明:1。发布地址,2。接口实例发布地址中IP是主机地址,其余可以自定义Endpoint。publish(http:192。168。0。103:8087myserviehello,newWebSEIImpl());System。out。println(发布成功!);}} 上面的main方法启动之后,可以在浏览器输入http:192。168。0。103:8087myserviehello?wsdl进行访问和查看wsdl文档。wsdl文档解析 文件格式大概如下:definitionstypesschemaelementmessagepartportTypeoperationinputoutputbindingoperationinputoutputservicetypesxsd:schemaxsd:importnamespacehttp:webservice。mine。comschemaLocationhttp:192。168。0。103:8087myserviehello?xsd1xsd:schematypes 上面这段是定义webservice的wsdl文档中引入的约束,其中schemaLocationhttp:192。168。0。103:8087myserviehello?xsd1是约束内容的地址,可以用浏览器进行访问查看,当然这需要一定schema约束相关的知识,可以事先了解下。messagenamesayHellopartnameparameterselementtns:sayHellomessagemessagenamesayHelloResponsepartnameparameterselementtns:sayHelloResponsemessage 上面的message是定义webservice所有的消息结构; 标签通过element属性指定标签里schema中的约束中的片段。portTypenameWebSEIImploperationnamesayHelloinputwsam:Actionhttp:webservice。mine。comWebSEIImplsayHelloRequestmessagetns:sayHellooutputwsam:Actionhttp:webservice。mine。comWebSEIImplsayHelloResponsemessagetns:sayHelloResponseoperationportType 上面的标签是用来定义服务器端的SEI; 标签用来指定一个webservice中的处理请求的方法; 指定客户端应用传过来的数据,会引用上面定义的; 指定服务器端返回给客户端的数据,会引用上面定义的;bindingnameWebSEIImplPortBindingtypetns:WebSEIImplsoap:bindingtransporthttp:schemas。xmlsoap。orgsoaphttpstyledocumentoperationnamesayHellosoap:operationsoapActioninputsoap:bodyuseliteralinputoutputsoap:bodyuseliteraloutputoperationbinding 用于定义SEI的实现类,type属性引用上面的; 用来定义方法与上面中的差不多,这里name属性是服务接口的方法名; 表示传输数据格式是文档(xml); 表示数据是文本数据;servicenameWebSEIImplServiceportnameWebSEIImplPortbindingtns:WebSEIImplPortBindingsoap:addresslocationhttp:192。168。0。103:8087myserviehelloportservice 是代表一个webservice容器,name属性指定webservice客户端的容器类 代表一个服务器端SEI的一个实现,bingding属性引用上面的标签,name属性是指定webservice客户端真正的所需要的服务类; 代表当前webservice的请求地址开发客户端 查看对应的wsdl文档(一般浏览器。。。。?wsdl) 请求webservice并查看请求和响应消息(一般浏览器) 创建客户端应用编码方式访问 借助jdk的wsimort。exe工具生成客户端代码:进入项目src目录下,打开cmd窗口,输入wsimportkeepurl(url是wsdl文件的路径或者是wsdl文档) 生成的代码类如下: 借助生成的代码编写请求代码publicclassClientTest{publicstaticvoidmain(String〔〕args){WebSEIImplServicefactorynewWebSEIImplService();WebSEIImplwebSEIfactory。getWebSEIImplPort();System。out。println(webSEI。sayHello(jack));}} 用cxf开发webservice用cxf比jdk的优势 jdk不支持map与复杂的object类型,cxf支持所有类型 cxf提供很多拦截器,后期业务拓展性高所需依赖:dependencygroupIdorg。apache。cxfgroupIdcxfcoreartifactIdversion3。4。4versiondependencydependencygroupIdorg。apache。cxfgroupIdcxfrtfrontendjaxwsartifactIdversion3。4。4versiondependencydependencygroupIdorg。apache。cxfgroupIdcxfrttransportshttpjettyartifactIdversion3。4。4versiondependency服务端代码示例:JaxWsServerFactoryBeanfactoryBeannewJaxWsServerFactoryBean();暴露地址factoryBean。setAddress(http:192。168。0。103:8087myserviehello);SEI接口factoryBean。setServiceClass(WebSEI2。class);SEI实现类factoryBean。setServiceBean(newWebSEI2Impl());factoryBean。create();System。out。println(发布成功!);创建客户端应用编码方式访问 首先可以在https:cxf。apache。orgdownload。html下载cxf压缩包,并且配置环境变量; 然后进入项目src目录下,打开cmd窗口,输入wsdl2javaurl(url是wsdl文件的路径或者是wsdl文档)生成客户端所需要的代码 或者maven的pom文件添加plugingroupIdorg。apache。cxfgroupIdcxfcodegenpluginartifactIdversion3。5。0versionexecutionsexecutionidgeneratesourcesidphasegeneratesourcesphaseconfigurationsourceRootgeneratedcxfsourceRootwsdlOptionswsdlOptionwsdlhttp:192。168。0。103:8087myserviehello?wsdlwsdlwsdlOptionwsdlOptionsconfigurationgoalsgoalwsdl2javagoalgoalsexecutionexecutionsplugin 然后运行 mvncleangeneratesources 这将生成wsdl对应的客户端代码,代码会存放于generatedcxf目录。 又或者直接写客户端代码:{JaxWsDynamicClientFactoryclientFactoryJaxWsDynamicClientFactory。newInstance();ClientclientclientFactory。createClient(http:192。168。0。103:8087myserviehello?wsdl);Object〔〕resultclient。invoke(sayHappy,KEVIN);System。out。println(result〔0〕);} 所需依赖:dependencygroupIdorg。apache。cxfgroupIdcxfcoreartifactIdversion3。4。4versiondependencydependencygroupIdorg。apache。cxfgroupIdcxfrtfrontendjaxwsartifactIdversion3。4。4versiondependencydependencygroupIdorg。apache。cxfgroupIdcxfrttransportshttpjettyartifactIdversion3。4。4versiondependency