




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、基于JAX-WS 開發(fā)Web Service2008-05-06基于JAX-WS 設計開發(fā) Web ServiceJAX-WS 概述JAX-WS發(fā)展歷史(JAX-WS2.0與JAX-RPC1.1的比較)如何基于JAX-WS API 開發(fā)Web Service (Apache CXF基礎(chǔ))JAX-WS 概述什么是JAX-WS?為何使用 JAX-WS?JAX-WS發(fā)展歷史 Web Service出現(xiàn)以來。首先是 SOAP,但 SOAP 僅描述消息的情況,然后是 WSDL,WSDL 并不會告訴您如何使用 Java 編寫 Web 服務。在這種情況下,JAX-RPC 1.0 應運而生。經(jīng)過數(shù)月使用之后,
2、編寫此規(guī)范的 Java Community Process (JCP) 人員認識到需要對其進行一些調(diào)整,調(diào)整的結(jié)果就是 JAX-RPC 1.1。該規(guī)范使用大約一年之后,JCP 人員希望構(gòu)建一個更好的版本:JAX-RPC 2.0。其主要目標是與行業(yè)方向保持一致,但行業(yè)中不僅只使用 RPC Web 服務,還使用面向消息的 Web 服務。因此從名稱中去掉了“RPC”,取而代之的是“WS”(當然表示的是 Web 服務)。因此 JAX-RPC 1.1 的后續(xù)版本是 JAX-WS 2.0Java API for XML-based Web services。JAX-WS2.0與JAX-RPC1.1的比較哪
3、些內(nèi)容保持不變?JAX-WS 2.0仍然支持 SOAP 1.1 over HTTP 1.1,因此互操作性將不會受到影響。 JAX-WS2.0 仍然支持 WSDL 1.1,因此WSDL 1.1的相關(guān)規(guī)范仍然適用。JAX-WS2.0與JAX-RPC1.1的比較區(qū)別和變化:SOAP 1.2 JAX-RPC 和 JAX-WS 都支持 SOAP 1.1。JAX-WS 還支持 SOAP 1.2。XML/HTTP WSDL 1.1 規(guī)范在 HTTP 綁定中定義,這意味著利用此規(guī)范可以在不使用 SOAP 的情況下通過 HTTP 發(fā)送 XML 消息。JAX-RPC 忽略了 HTTP 綁定。而 JAX-WS 添加
4、了對其的支持。WS-I Basic Profile JAX-RPC 支持 WS-I Basic Profile (BP) V1.0,JAX-WS 支持 BP 1.1。(WS-I 即 Web 服務互操作性組織。)新 Java 功能 JAX-RPC 映射到 Java 1.4,JAX-WS 映射到 Java 5.0。JAX-WS 依賴于 Java 5.0 中的很多新功能。 Java EE 5 是 J2EE 1.4 的后續(xù)版本,添加了對 JAX-WS 的支持,但仍然支持 JAX-RPC。JAX-WS2.0與JAX-RPC1.1的比較數(shù)據(jù)映射模型 JAX-RPC 具有自己的映射模型,此模型大約涵蓋了所有
5、模式類型中的 90%。JAX-WS 的數(shù)據(jù)映射模型是 JAXB。JAXB 可保證所有 XML 模式的映射。 接口映射模型 JAX-WS 的基本接口映射模型與 JAX-RPC 的區(qū)別并不大,不過二者之間存在以下差異:JAX-WS 的模型使用新的 Java 5.0 特性Annotations 。 JAX-WS 的模型引入了異步功能。 動態(tài)編程模型 JAX-WS 的動態(tài)客戶機模型與 JAX-RPC 的對應模型差別很大。很多更改都是為了認可行業(yè)需求: 引入了面向消息的功能。 引入了動態(tài)異步功能。 JAX-WS 還添加了動態(tài)服務器模型,而 JAX-RPC 則沒有此模型。 消息傳輸優(yōu)化機制(Message
6、 Transmission Optimization Mechanism,MTOM) JAX-WS 通過 JAXB 添加了對新附件規(guī)范 MTOM 的支持。JAX-WS 兩種開發(fā)方式契約優(yōu)先(WSDL First):從 WSDL 著手,生成 Java 類來實現(xiàn)服務。 代碼優(yōu)先 (JAVA First):從 Java 類著手,使用 Annotation 來生成 WSDL 文件和 Java 接口。Apache CXF 基礎(chǔ)Apache CXF簡介: Apache CXF: An Open Source Service Framework Apache CXF 是一個Service框架,它簡化了Ser
7、vice的創(chuàng)建, 實現(xiàn)了JAX-WS2.0規(guī)范,并通過了JAX-WS2.0 TCK; CXF和Spring無縫集成;Apache CXF 基礎(chǔ)Apache CXF功能特性靈活部署輕量級容器:可在 Tomcat 或基于 Spring 的容器中部署 Services; 集成 JBI:可以在如 ServiceMix, OpenESB or Petals 等等的 JBI 容器中將它部署為一個服務引擎; 集成 SCA:可以部署在如 Tuscany 之類的 SCA 容器中; 集成 J2EE:可以在 J2EE 應用服務器中部署 Services,比如:Geronimo、JOnAS、JBoss、WebSphe
8、re Application Server 和 WebLogic Application Server,以及 Jetty 和 Tomcat; 獨立的 Java 客戶端服務器。 支持多種編程語言全面支持 JAX-WS 2.0 客戶端服務器編程模型; 支持 JAX-WS 2.0 synchronous、asynchronous 和 one-way APIs; 支持 JAX-WS 2.0 Dynamic Invocation Interface (DII) API; 支持 wrapped and non-wrapped 風格; 支持 XML messaging API; 支持 JavaScript
9、和 ECMAScript 4 XML (E4X) ,客戶端與服務端均支持; 通過 Yoko 支持 CORBA; 通過 Tuscany 支持 SCA; 通過 ServiceMix 支持 JBI ; 契約優(yōu)先(WSDL First)完成WSDL文件示例: AccountManagerService.wsdlWSDL文件WSDL文件WSDL文件契約優(yōu)先(WSDL First)利用Apache CXF 的WSDL2JAVA 工具生成java類 注意:環(huán)境變量path增加: %CXF_HOME%bin輸入命令: D:workspacewsdlwsdl2java -client -d SrcDir Acc
10、ountManagerService.wsdl契約優(yōu)先(WSDL First)生成代碼列表:契約優(yōu)先(WSDL First)分析生成JAVA類1、根據(jù)WSDL中不同Name 的Message節(jié)點,生成對應類型的Schema的類:CheckBalance.java, CheckBalanceResponse.javaWithdraw.java,WithdrawResponse.java示例:分析生成JAVA類3、生成對象工廠類ObjectFactory.java和包信息類 package-info.java契約優(yōu)先(WSDL First)4、生成客戶端訪問服務的Service類AccountMa
11、nagerService.java該類繼承: javax.xml.ws.Service契約優(yōu)先(WSDL First)契約優(yōu)先(WSDL First)根據(jù)生成PortType接口類編寫具體服務實現(xiàn)類(Server端)1、編寫服務實現(xiàn)類: AccountManagerServiceImpl.java該類實現(xiàn)接口AccountManagerServicePortType.java契約優(yōu)先(WSDL First)根據(jù)生成PortType接口類編寫具體服務實現(xiàn)類(Server端)2、編寫服務啟動類Server.java,運行該類啟動發(fā)布服務;使用javax.xml.ws.Endpoint類的publi
12、sh方法示例:契約優(yōu)先(WSDL First)根據(jù)生成的客戶端binding測試類 來測試具體服務運行測試類對服務進行訪問:AccountManagerServicePortType_AccountManagerServiceSOAP11PortHttp_Client.java基于JAX-WS API 開發(fā)Web ServiceJAX-WS 客戶端APIsJAX-WS 服務端APIs基于JAX-WS API 開發(fā)Web ServiceJAX-WS 客戶端APIsjavax.xml.ws.Servicejavax.xml.ws.BindingProviderjavax.xml.ws.Dispat
13、ch基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.ServiceService類提供對WebService的客戶端訪問方法主要方法:構(gòu)造方法:Service(.URLwsdlDocumentLocation, space.QNameserviceName)addPort(space.QNameportName, java.lang.StringbindingId, java.lang.StringendpointAddress) create(.URLwsdlDocumentLocation, space.QNameserviceName) getPort(ja
14、va.lang.ClassserviceEndpointInterface) createDispatch(EndpointReferenceendpointReference, java.lang.Classtype, Service.Modemode, WebServiceFeature features) setExecutor(java.util.concurrent.Executorexecutor)setHandlerResolver(HandlerResolverhandlerResolver)javax.xml.ws.Service示例1: private static fin
15、al QName SERVICE_NAME = new QName(http:/server.java_first_jaxws.hw.demo/, HelloWorld); private static final QName PORT_NAME = new QName(http:/server.java_first_jaxws.hw.demo/, HelloWorldPort); Service service = Service.create(SERVICE_NAME); / Endpoint Address String endpointAddress = http:/localhost
16、:9000/helloWorld; / Add a port to the Service service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); HelloWorld hw = service.getPort(HelloWorld.class); System.out.println(hw.sayHi(World);javax.xml.ws.Service示例2: URL wsdlLocation = new URL(/my.wsdl); QName serviceName = new QNam
17、e(/sample, MyService); Service s = Service.create(wsdlLocation, serviceName); HelloWorld hw = service.getPort(HelloWorld.class); System.out.println(hw.sayHi(World);基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.BindingProvider The BindingProvider interface provides access to the protocol binding and associa
18、ted context objects for request and response message processing.javax.xml.ws.BindingProvider示例: javax.xml.ws.Service service = .;com.example.StockQuoteProvider proxy = service.getPort(portName, com.example.StockQuoteProvider.class) javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)prox
19、y; Map context = bp.getRequestContext(); context.setProperty(javax.xml.ws.session.maintain, Boolean.TRUE); proxy.getLastTradePrice(ACME);基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.Dispatch The Dispatch interface provides support for the dynamic invocation of a service endpoint operations. The javax.xml.
20、ws.Service interface acts as a factory for the creation of Dispatch instances. javax.xml.ws.Dispatch示例1: Synchronous, Payload-Oriented Source reqMsg = .; Service service = .; Dispatch disp = service.createDispatch(portName, Source.class, PAYLOAD); Source resMsg = disp.invoke(reqMsg);示例2 Synchronous,
21、 Message-Oriented SOAPMessage soapReqMsg = .; Service service = .; Dispatch disp = service.createDispatch(portName, SOAPMessage.class, MESSAGE); SOAPMessage soapResMsg = disp.invoke(soapReqMsg);javax.xml.ws.Dispatch示例3 Synchronous, Payload-Oriented With JAXB Objects JAXBContext jc = JAXBContext.newI
22、nstance(primer.po); Unmarshaller u = jc.createUnmarshaller(); PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream( po.xml ) ); Service service = .; Dispatch disp = service.createDispatch(portName, jc, PAYLOAD); OrderConfirmation conf = (OrderConfirmation)disp.invoke(po);javax.xml.ws.D
23、ispatch示例4 Asynchronous, Polling, Message-Oriented SOAPMessage soapReqMsg = .; Service service = .; Dispatch disp = service.createDispatch(portName, SOAPMessage.class, MESSAGE); Response res = disp.invokeAsync(soapReqMsg); while (!res.isDone() / do something while we wait SOAPMessage soapResMsg = re
24、s.get();javax.xml.ws.Dispatch示例5 Asynchronous, Callback, Payload-Orientedclass MyHandler implements AsyncHandler public void handleResponse(Response res) Source resMsg = res.get();/ do something with the results Source reqMsg = .; Service service = .; Dispatch disp = service.createDispatch(portName,
25、Source.class, PAYLOAD); MyHandler handler = new MyHandler(); disp.invokeAsync(reqMsg, handler);基于JAX-WS API 開發(fā)Web ServiceJAX-WS 服務端APIsjavax.xml.ws.Endpointjavax.xml.ws.Providerjavax.xml.ws.WebServiceContext基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.EndpointA Web service endpoint. Endpoints are created
26、using the static methods defined in this class. An endpoint is always tied to one Binding and one implementor, both set at endpoint creation time. javax.xml.ws.Endpointjavax.xml.ws.Endpointpublish(java.lang.Stringaddress, java.lang.Objectimplementor) Creates and publishes an endpoint for the specifi
27、ed implementor object at the given address.getBinding() Returns the binding for this endpoint.setExecutor(java.util.concurrent.Executorexecutor) Sets the executor for this Endpoint instance.setMetadata(java.util.Listmetadata) Sets the metadata for this endpoint.javax.xml.ws.Endpoint示例1:protected Ser
28、ver() throws Exception System.out.println(Starting Server); Object implementor = new AccountManagerServiceImpl(); String address = :9000/services/AccountManagerService; Endpoint.publish(address, implementor); javax.xml.ws.Endpoint示例2: / assume Test is an endpoint implementation class annotated with
29、WebService Test test = new Test();HttpServer server = HttpServer.create(new InetSocketAddress(8080),10); server.setExecutor(Executor.newFixedThreadPool(10); server.start(); HttpContext context = server.createContext(/test); Endpoint endpoint = Endpoint.create(SOAPBinding.SOAP11HTTP_BINDING, test); e
30、ndpoint.publish(context);基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.Provider The Provider interface offers an alternative to SEIs and may be implemented by services wishing to work at the XML message level.javax.xml.ws.Provider主要方法:createServiceDelegate(.URLwsdlDocumentLocation, space.QNameserviceName,
31、java.lang.ClassserviceClass) Creates a service delegate object.createEndpoint(java.lang.StringbindingId, java.lang.Objectimplementor) Creates an endpoint object with the provided binding and implementation object.createAndPublishEndpoint(java.lang.Stringaddress, java.lang.Objectimplementor) Creates
32、and publishes an endpoint object with the specified address and implementation object.getPort(EndpointReferenceendpointReference, java.lang.ClassserviceEndpointInterface, WebServiceFeature.features) The getPort method returns a proxy.javax.xml.ws.Provider示例1: Simple echo service, reply message is th
33、e same as the input message WebServiceProvider ServiceMode(value=Service.Mode.MESSAGE) public class MyService implements Provider public MyService() public SOAPMessage invoke(SOAPMessage request) return request; javax.xml.ws.Provider示例2: Simple static reply, reply message contains a fixed acknowlegm
34、ent element WebServiceProvider ServiceMode(value=Service.Mode.PAYLOAD) public class MyService implements Provider public MyService() public Source invoke(Source request) Source requestPayload = request.getPayload();String replyElement = ; StreamSource reply = new StreamSource(new StringReader(replyE
35、lement); return reply; javax.xml.ws.Provider示例3: Using JAXB to read the input message and set the replyWebServiceProviderServiceMode(value=Service.Mode.PAYLOAD)public class MyService implements Provider public MyService() public Source invoke(Source request) JAXBContent jc = JAXBContext.newInstance(
36、.); Unmarshaller u = jc.createUnmarshaller(); Object requestObj = u.unmarshall(request);Acknowledgement reply = new Acknowledgement(.); return new JAXBSource(jc, reply); 基于JAX-WS API 開發(fā)Web Servicejavax.xml.ws.WebServiceContext A WebServiceContext makes it possible for a web service endpoint implemen
37、tation class to access message context and security information relative to a request being served. Typically a WebServiceContext is injected into an endpoint implementation class using the Resource annotation. javax.xml.ws.WebServiceContext示例1:WebServicepublic class Test Resourceprivate WebServiceContext context;public String reverse(String inputString) /根據(jù)WebServiceCon
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外籍工作人員聘請合同范本
- 綠化養(yǎng)護及道路保潔服務合同范本
- 關(guān)于英語合同范本
- 介紹公司合同范本
- 二包服務合同范本
- 臨時設施合同范本
- weiyu供銷合同范本
- 會計簽合同范例
- 合作合同范例版
- 公司免責合同范本
- 2025年湖南司法警官職業(yè)學院單招職業(yè)傾向性測試題庫學生專用
- 2025年呼和浩特職業(yè)學院單招職業(yè)傾向性測試題庫及參考答案
- 醫(yī)學遺傳學教案-山東大學醫(yī)學遺傳學
- 四川德陽歷年中考語文文言文閱讀試題12篇(含答案與翻譯)(截至2024年)
- 合唱之美知到智慧樹章節(jié)測試課后答案2024年秋山東航空學院
- 海南省澄邁縣2024-2025學年七年級上學期期末考試地理試題(含答案)
- 食品安全演練預案及流程
- 2025年蘇州衛(wèi)生職業(yè)技術(shù)學院高職單招職業(yè)技能測試近5年常考版參考題庫含答案解析
- 2025屆威海市高三語文上學期期末考試卷附答案解析
- 新能源汽車充電設施建設規(guī)劃與管理計劃
評論
0/150
提交評論