Lecture 7 Examples for Web Service 苏伟 [email protected].
-
Upload
august-strickland -
Category
Documents
-
view
277 -
download
0
Transcript of Lecture 7 Examples for Web Service 苏伟 [email protected].
Lecture 7Lecture 7Examples for Web ServiceExamples for Web Service
Part I JAX-WSPart I JAX-WS
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Quick Overview of JAX-WS 2.1Quick 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-WSJAX-WS Core specification for a newly redesigned API stack f
or 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 ServiceTwo 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)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
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Example 1: Servlet-Based EndpointExample 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
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Example 2: EJB-Based EndpointExample 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...
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Customizing through AnnotationsCustomizing 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 ...
}
}
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Java SE Client-Side ProgrammingJava 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<ServiceName>Port method
5. Invoke any remote operations
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Example: Java SE-Based ClientExample: 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 ProgrammingJava 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
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Example: Java EE-Based ClientExample: Java EE-Based Client
@Stateless
public class MyBean {
// Resource injection
@WebServiceRef(CalculatorService.class)
Calculator proxy;
public int mymethod() {
return proxy.add(35, 7);
}
SOA & Web Service
Lan Zhou University
Bottom-up development toolingBottom-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 JavaBeansCreating 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 ExplorerTesting with the Web Services Explorer
SOA & Web Service
Lan Zhou University
Useful URLsUseful 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
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Annotations Used in JAX-WSAnnotations 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专业综合改革项目
@WebService@WebService Marks a Java class as implementing a Web Service, o
r 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专业综合改革项目
Example: @WebService propertiesExample: @WebService properties
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
@WebMethod@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.
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Example: @WebMethod propertiesExample: @WebMethod properties
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
@WebParam@WebParam
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专业综合改革项目
@WebResult@WebResult
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专业综合改革项目
ExampleExample
@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;
}
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Protocol and Transport IndependenceProtocol 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
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
ExampleExample
@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public class AddNumbersImpl {
// More code
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
Handler TypesHandler 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 HandlerLogical Handler
public class MyLogicalHandler implements LogicalHandler<LogicalMessageContext> {
public boolean handleMessage(LogicalMessageContext messageContext) {
LogicalMessage msg = messageContext.getMessage();
return true;
}
// other methods
}
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
SOAP HandlerSOAP Handler
public class MySOAPHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext messageContext) {
SOAPMessage msg = messageContext.getMessage();
return true;
}
// other methods
}
Part IIPart II手机短信发布平台手机短信发布平台 Web ServiceWeb Service 开发实例开发实例
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
利用利用 WebServiceWebService 实现异构系统方法的调用实现异构系统方法的调用
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
基于基于 JAX-WSJAX-WS 的的 WebServiceWebService 开发开发
package com.cwq.ws;
public class SayHello{
public string SayHello(String name){
Return "Hello"+name;
}
}
SOA & Web Service
Lan Zhou University
教育部 -IBM专业综合改革项目
发布手机短信发布手机短信 web service 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 Dotnet web service 客户端客户端代码如下:
– Localhost.MsgService svr=new eshop.localhost.MsgService();
– svr.send(tel,"您在 eshop的密码已找回,新密码是 "+newPassword);
运行结果如下:
SOA & Web Service
Lan Zhou University
Thank you!