Lecture 7 Examples for Web Service 苏伟
Part I JAX-WS
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Quick Overview of JAX-WS 2.1 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.1 Protocol and transport independence
SOA & Web Service Lan Zhou University JAX-WS Core specification for a newly redesigned API stack for Web services – Includes JAX-WS V2.1, JAXB V2.1, SAAJ V1.3 and StAX V1.0
SOA & Web Service Lan Zhou University 教育部 -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
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Server-Side Programming Model:(Starting from POJO) 1.Write a POJO implementing the service annotation to it 3.Optionally, inject a WebServiceContext 4.Deploy the application 5.Point your clients at the WSDL e.g.
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example 1: Servlet-Based public class Calculator { public int add(int a, int b) { return a+b; } annotation –All public methods become web service operations WSDL/Schema generated automatically –Default values are used
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example 2: public class Calculator WebServiceContext context; public int add(int a, int b) { return a+b; } It’s a regular EJB 3.0 component, so it can use any EJB features –Transactions, security, interceptors...
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Customizing through targetNamespace=” public class CreditRating public Score Customer c) { //... implementation code... }
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Java SE Client-Side Programming 1.Point a tool (NetBeans or wsimport) at the WSDL for the service wsimport 2.Generate annotated classes and interfaces 3.Call new on the service class 4.Get a proxy using a get Port method 5.Invoke any remote operations
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example: Java SE-Based Client CalculatorService svc = new CalculatorService(); Calculator proxy = svc.getCalculatorPort(); int answer = proxy.add(35, 7); No need to use factories The code is fully portable XML is completely hidden from programmer
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Java EE Client-Side Programming 1.Point a tool (NetBeans or wsimport) at the WSDL for the service wsimport 2.Generate annotated classes and interfaces 3.Inject of the appropriate type No JNDI needed 4.Invoke any remote operations
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example: Java EE-Based public class MyBean { // Resource Calculator proxy; public int mymethod() { return proxy.add(35, 7); }
SOA & Web Service Lan Zhou University Bottom-up development tooling IBM Rational Application Developer V7.5 provides wizards to create complete Web service applications from: – JavaBeans – Stateless session EJBs – JPA entities – SQL statements The wizards can: – Generate source code for services – Generate source code for service clients (including Web pages) – Generate WSDL – Assemble, deploy, install, start and test the service – Assemble, deploy, install, start and test service clients
SOA & Web Service Lan Zhou University Creating a Web service from JavaBeans Configure and start a server Create or import a bean into the source folder of a Java project or dynamic Web project Switch to the Java EE perspective Select the bean in the Enterprise Explorer Invoke the Web services wizard by choosing either: Web Services > Create Web Services from context menu File > New > Other > Web Services > Web Service from the workbench menu
SOA & Web Service Lan Zhou University Testing with the Web Services Explorer
SOA & Web Service Lan Zhou University Useful URLs – Displays the namespace of the service and a greeting – Mostly used to verify service is running – Retrieves the dynamically generated WSDL for the service servicename_schema1.xsd – Retrieves the XML schema for the WSDL Examples: – – –
SOA & Web Service Lan Zhou University 教育部 -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
SOA & Web Service Lan Zhou University 教育部 -IBM Marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface. Attributes –endpointInterface –name the value of wsdl:portType –portName the value of wsdl:portName –serviceName the value of wsdl:service –targetNamespace –wsdlLocation
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 properties
SOA & Web Service Lan Zhou University 教育部 -IBM Customizes a method that is exposed as a Web Service operation Attributes –operationName Specifies the name of the wsdl:operation that for this method. The default value is the name of the Java method. –action Defines the value (a URI) of the SOAPAction header in HTTP. This gives the receiving servlet a hint as to the intent of the message. –exclude If true, indicates that the method should be excluded from the Web service. The default value is false.
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 properties
SOA & Web Service Lan Zhou University 教育部 -IBM Customizes the mapping of an individual parameter to a Web Service message part and XML element. Attributes >header >mode >name >partName >targetNamespace
SOA & Web Service Lan Zhou University 教育部 -IBM Customizes the mapping of the return value to a WSDL part and XML element. Attributes >header >name >partName >targetNamespace
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 = " use=SOAPBinding.Use.LITERAL) public interface AddNumbersIF public int number2) throws AddNumbersException; }
SOA & Web Service Lan Zhou University 教育部 -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, 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
SOA & Web Service Lan Zhou University 教育部 -IBM DING) public class AddNumbersImpl { // More code
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Handler Types JAX-WS 2.0 defines a Handler interface, with subinterfaces LogicalHandler and SOAPHandler. >The Handler interface contains >handleMessage(C context) >handleFault(C context) >C extends MessageContext >A property in the MessageContext 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
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Logical Handler public class MyLogicalHandler implements LogicalHandler { public boolean handleMessage(LogicalMessageContext messageContext) { LogicalMessage msg = messageContext.getMessage(); return true; } // other methods }
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 SOAP Handler public class MySOAPHandler implements SOAPHandler { public boolean handleMessage(SOAPMessageContext messageContext) { SOAPMessage msg = messageContext.getMessage(); return true; } // other methods }
Part II 手机短信发布平台 Web Service 开发实例
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 利用 WebService 实现异构系统方法的调用
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 基于 JAX-WS 的 WebService 开发 package com.cwq.ws; public class SayHello{ public string SayHello(String name){ Return "Hello"+name; }
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 发布手机短信 web service 服务 Windows/system32/FT_ET99_API.dll JET99A120.dll %JDK%/JRE/lib/ext/RxTxComm.jar %tomcat%/bin/FT_ET99_API.dll JET99A120.dll RxTxParallel.dll rxtxSerial.dll Szhtogsm.jar RxTxComm.jar Eclipse: Szhtogsm.jar RxTxComm.jar
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Dotnet web service 客户端 代码如下: –Localhost.MsgService svr=new eshop.localhost.MsgService(); –svr.send(tel," 您在 eshop 的密码已找回,新密码是 "+newPassword); 运行结果如下:
SOA & Web Service Lan Zhou University Thank you !