Download presentation
Presentation is loading. Please wait.
1
Lecture 7 Examples for Web Service
苏伟
2
Part I JAX-WS
3
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
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
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
Server-Side Programming Model:(Starting from POJO)
Write a POJO implementing the service annotation to it Optionally, inject a WebServiceContext Deploy the application Point your clients at the WSDL e.g.
7
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
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
Customizing through Annotations
@WebService(name=”CreditRatingService”, targetNamespace=” public class CreditRating { @WebMethod(operationName=”getCreditScore”) public Score getCredit( @WebParam(name=”customer”) Customer c) { // ... implementation code ... }
10
Java SE Client-Side Programming
Point a tool (NetBeans or wsimport) at the WSDL for the service wsimport Generate annotated classes and interfaces Call new on the service class Get a proxy using a get<ServiceName>Port method Invoke any remote operations
11
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
Java EE Client-Side Programming
Point a tool (NetBeans or wsimport) at the WSDL for the service wsimport Generate annotated classes and interfaces Inject of the appropriate type No JNDI needed Invoke any remote operations
13
Example: Java EE-Based Client
@Stateless public class MyBean { // Resource Calculator proxy; public int mymethod() { return proxy.add(35, 7); }
14
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
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
Testing with the Web Services Explorer
17
Useful URLs http://host:port/webroot/servicename
– 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: – – –
18
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
@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
Example: @WebService properties
21
@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
Example: @WebMethod properties
23
@WebParam Customizes the mapping of an individual parameter to a Web Service message part and XML element. Attributes >header >mode >name >partName >targetNamespace
24
@WebResult Customizes the mapping of the return value to a WSDL part and XML element. Attributes >header >name >partName >targetNamespace
25
Example @WebService(targetNamespace = " use=SOAPBinding.Use.LITERAL) public interface AddNumbersIF public int number2) throws AddNumbersException; }
26
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
Example public class AddNumbersImpl { // More code
28
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
Logical Handler public class MyLogicalHandler implements LogicalHandler<LogicalMessageContext> { public boolean handleMessage(LogicalMessageContext messageContext) { LogicalMessage msg = messageContext.getMessage(); return true; } // other methods
30
SOAP Handler public class MySOAPHandler implements SOAPHandler<SOAPMessageContext> { public boolean handleMessage(SOAPMessageContext messageContext) { SOAPMessage msg = messageContext.getMessage(); return true; } // other methods
31
Part II 手机短信发布平台Web Service开发实例
32
利用WebService实现异构系统方法的调用
33
基于JAX-WS的WebService开发
package com.cwq.ws; public class SayHello{ public string SayHello(String name){ Return "Hello"+name; }
34
发布手机短信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:
35
Dotnet web service 客户端 代码如下: 运行结果如下:
Localhost.MsgService svr=new eshop.localhost.MsgService(); svr.send(tel,"您在eshop的密码已找回,新密码是"+newPassword); 运行结果如下:
36
Thank you!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.