Download presentation
Presentation is loading. Please wait.
1
Lecture 7 Examples for Web Service 苏伟 suweilzu@eyou.com
2
Part I JAX-WS
3
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
4
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
5
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
6
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Server-Side Programming Model:(Starting from POJO) 1.Write a POJO implementing the service 2.Add @WebService annotation to it 3.Optionally, inject a WebServiceContext 4.Deploy the application 5.Point your clients at the WSDL e.g. http://myserver/myapp/MyService?WSDL
7
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example 1: Servlet-Based Endpoint @WebService public class Calculator { public int add(int a, int b) { return a+b; } @WebService annotation –All public methods become web service operations WSDL/Schema generated automatically –Default values are used
8
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example 2: EJB-Based Endpoint @WebService @Stateless public class Calculator { @Resource 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...
9
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Customizing through Annotations @WebService(name=”CreditRatingService”, targetNamespace=”http://example.org”) public class CreditRating { @WebMethod(operationName=”getCreditScore”) public Score getCredit( @WebParam(name=”customer”) Customer c) { //... implementation code... }
10
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 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 Port method 5.Invoke any remote operations
11
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
12
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 http://example.org/calculator.wsdl 2.Generate annotated classes and interfaces 3.Inject a @WebServiceReference of the appropriate type No JNDI needed 4.Invoke any remote operations
13
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example: Java EE-Based Client @Stateless public class MyBean { // Resource injection @WebServiceRef(CalculatorService.class) Calculator proxy; public int mymethod() { return proxy.add(35, 7); }
14
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
15
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
16
SOA & Web Service Lan Zhou University Testing with the Web Services Explorer
17
SOA & Web Service Lan Zhou University Useful URLs http://host:port/webroot/servicename – Displays the namespace of the service and a greeting – Mostly used to verify service is running http://host:port/webroot/servicename?wsdl – Retrieves the dynamically generated WSDL for the service http://host:port/webroot/servicename/ servicename_schema1.xsd – Retrieves the XML schema for the WSDL Examples: – http://host.net:9080/MyWeb/MySvc – http://host.net:9080/MyWeb/MySvc?wsdl – http://host.net:9080/MyWeb/MySvc/MySvc_schema1.xsd
18
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
19
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 @WebService 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
20
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example: @WebService properties
21
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 @WebMethod 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.
22
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example: @WebMethod properties
23
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 @WebParam Customizes the mapping of an individual parameter to a Web Service message part and XML element. Attributes >header >mode >name >partName >targetNamespace
24
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 @WebResult Customizes the mapping of the return value to a WSDL part and XML element. Attributes >header >name >partName >targetNamespace
25
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example @WebService(targetNamespace = "http://duke.example.org", name="AddNumbers") @SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL) public interface AddNumbersIF { @WebMethod(operationName="add", action="urn:addNumbers") @WebResult(name="return") public int addNumbers( @WebParam(name="num1")int number1, @WebParam(name="num2")int number2) throws AddNumbersException; }
26
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, e.g. @BindingType(SOAPBinding.SOAP12HTTP_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
27
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Example @WebService @BindingType(value=SOAPBinding.SOAP12HTTP_BIN DING) public class AddNumbersImpl { // More code
28
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
29
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 }
30
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 }
31
Part II 手机短信发布平台 Web Service 开发实例
32
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 利用 WebService 实现异构系统方法的调用
33
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; }
34
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
35
SOA & Web Service Lan Zhou University 教育部 -IBM 专业综合改革项目 Dotnet web service 客户端 代码如下: –Localhost.MsgService svr=new eshop.localhost.MsgService(); –svr.send(tel," 您在 eshop 的密码已找回,新密码是 "+newPassword); 运行结果如下:
36
SOA & Web Service Lan Zhou University Thank you !
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.