Lecture 6 Examples for Web Service suweilzueyou com
Lecture 6 Examples for Web Service 苏伟 suweilzu@eyou. com
Part I JAX-WS
SOA & Web Service 教育部-IBM���合改革�目 Quick Overview of JAX-WS 2. 0 § Simpler way to develop/deploy Web services >Plain Old Java Object (POJO) can be easily exposed as a Web service >No deployment descriptor is needed - use Annotation instead >Layered programming model § Part of Java SE 6 and Java EE 5 platforms § Integrated data binding via JAXB 2. 0 § Protocol and transport independence Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Layered Programming Model Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Two ways to create a Web Service § Starting from a WSDL file (top-down approach) >Generate classes using wsimport >WS interface >WS implementation skeleton class >Add business logic to the WS implementation class >Build, deploy, and test § Starting from a POJO (bottom-up approach) >Annotate POJO >Build and deploy >WSDL file generated automatically Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Server-Side Programming Model: (Starting from POJO) 1 Write a POJO implementing the service 2 Add @Web. Service annotation to it 3 Optionally, inject a Web. Service. Context 4 Deploy the application 5 Point your clients at the WSDL >e. g. http: //myserver/myapp/My. Service? WSDL Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Customizing through Annotations @Web. Service(name=”Credit. Rating. Service”, target. Namespace=”http: //example. org”) public class Credit. Rating { @Web. Method(operation. Name=”get. Credit. Score”) public Score get. Credit( @Web. Param(name=”customer”) Customer c) { //. . . implementation code. . . } } Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Java SE Client-Side Programming 1. Point a tool (Net. Beans or wsimport) at the WSDL for the service wsimport http: //example. org/calculator. wsdl 2. Generate annotated classes and interfaces 3. Call new on the service class 4. Get a proxy using a get<Service. Name>Port method 5. Invoke any remote operations Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Example: Java SE-Based Client Calculator. Service svc = new Calculator. Service(); Calculator proxy = svc. get. Calculator. Port(); int answer = proxy. add(35, 7); § No need to use factories § The code is fully portable § XML is completely hidden from programmer Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Java EE Client-Side Programming 1. Point a tool (Net. Beans or wsimport) at the WSDL for the service wsimport http: //example. org/calculator. wsdl 2. Generate annotated classes and interfaces 3. Inject a @Web. Service. Reference of the appropriate type • No JNDI needed 4. Invoke any remote operations Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Annotations Used in JAX-WS § JSR 181: Web Services Metadata for the Java Platform § JSR 222: Java Architecture for XML Binding (JAXB) § JSR 224: Java API for XML Web Services (JAX-WS) § JSR 250: Common Annotations for the Java Platform Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 @Web. Service § Marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface. § Attributes >endpoint. Interface >name >port. Name >service. Name >target. Namespace >wsdl. Location Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 @Web. Method § Customizes a method that is exposed as a Web Service operation § The method is not required to throw java. rmi. Remote. Exception. § Attributes >action >exclude >operation. Name Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 @Web. Param § Customizes the mapping of an individual parameter to a Web Service message part and XML element. § Attributes >header >mode >name >part. Name >target. Namespace Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 @Web. Result § Customizes the mapping of the return value to a WSDL part and XML element. § Attributes >header >name >part. Name >target. Namespace Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Protocol and Transport Independence § Typical application code is protocol-agnostic § Default binding in use is SOAP 1. 1/HTTP § Server can specify a different binding, e. g. @Binding. Type(SOAPBinding. SOAP 12 HTTP_BINDI NG) § Client must use binding specified in WSDL § Bindings are extensible, expect to see more of them >e. g. SOAP/Java Message Service(JMS) or XML/SMTP Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Handler Types § JAX-WS 2. 0 defines a Handler interface, with subinterfaces Logical. Handler and SOAPHandler. >The Handler interface contains >handle. Message(C context) >handle. Fault(C context) >C extends Message. Context >A property in the Message. Context object is used to determine if the message is inbound or outbound § SOAPHandler objects have access to the full soap message including headers § Logical handlers are independent of protocol and have access to the payload of the message Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Logical Handler public class My. Logical. Handler implements Logical. Handler<Logical. Message. Context> { public boolean handle. Message(Logical. Message. Context message. Context) { Logical. Message msg = message. Context. get. Message(); return true; } // other methods } Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 SOAP Handler public class My. SOAPHandler implements SOAPHandler<SOAPMessage. Context> { public boolean handle. Message(SOAPMessage. Context message. Context) { SOAPMessage msg = message. Context. get. Message(); return true; } // other methods } Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 利用Web. Service实现异构系统方法的调用 Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 基于JAX-WS的Web. Service开发 package com. cwq. ws; public class Say. Hello{ public string Say. Hello(String name){ Return "Hello"+name; } } Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 发布手机短信web service 服务 Windows/system 32/FT_ET 99_API. dll JET 99 A 120. dll %JDK%/JRE/lib/ext/Rx. Tx. Comm. jar %tomcat%/bin/FT_ET 99_API. dll Rx. Tx. Parallel. dll Szhtogsm. jar JET 99 A 120. dll rxtx. Serial. dll Rx. Tx. Comm. jar Eclipse: Szhtogsm. jar Rx. Tx. Comm. jar Lan Zhou University
SOA & Web Service 教育部-IBM���合改革�目 Dotnet web service 客户端 § 代码如下: – Localhost. Msg. Service svr=new eshop. localhost. Msg. Service(); – svr. send(tel, "您在eshop的密码已找回,新密码是 "+new. Password); § 运行结果如下: Lan Zhou University
SOA & Web Service Thank you! Lan Zhou University
- Slides: 25