+ All Categories
Home > Documents > Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web...

Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web...

Date post: 05-May-2018
Category:
Upload: vobao
View: 218 times
Download: 5 times
Share this document with a friend
21
Web Services Web Services
Transcript
Page 1: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Web ServicesWeb Services

Page 2: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Web ServicesWeb ServicesWeb services are a middleware, like CORBA and RMI.What makes web services unique is that the language being used is XMLThis is good for several reasons:This is good for several reasons:

Debugging is possible using a text editorPlatform independence since XML is just textPlatform independence, since XML is just textDevelopment of web services middleware is simple, since XML is easy to parse & generatesimple, since XML is easy to parse & generate

Page 3: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

SOAP

The XML language used by web services is called SOAP

The Simple Object Access ProtocoljOther middleware technologies use different communication languagesdifferent communication languages

IIOP is the wire protocol used by CORBAJRMP is the wire protocol used by RMIJRMP is the wire protocol used by RMIThese languages are binary, however

Page 4: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

SOAP

Simple Object Access ProtocolSOAP is the language used (over HTTP) for web services requests and responsesThi i h lik h HTML i d fThis is much like how HTML is used for traditional web responses

SOAP is usually hidden from developersSOAP is usually hidden from developers, but developers may examine the SOAP code for debugging purposescode for debugging purposes

SOAP is easy to understand since it is XML

Page 5: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

How SOAP WorksHow SOAP Works

The client sends a SOAP message (called a g (SOAP envelope) using HTTP to the serviceThe service is installed into a web server, which

i th HTTP t d threceives the HTTP request and passes the message to the serviceThe service processes the requested operationThe service processes the requested operation, and returns another SOAP envelope as the HTTP responseThe client reads and interprets the response SOAP envelope, and reacts to it somehow

Page 6: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

How SOAP Works (Contd )How SOAP Works (Contd.)

SOAP Request Envelope

ClientHTTP Server

SOAP Request Envelope

SOAP Response EnvelopeClientWeb

Service

p p

Page 7: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

How SOAP Works (Contd.)( )

One major advantage that SOAP has is j gthat it uses HTTP, which is a protocol already prevalent on the web

It is used for web browser/web server communication

T i ll SOAP li t h tTypically, SOAP clients, however, are not users, but other programs

SOAP clients are typically similar in nature toSOAP clients are typically similar in nature to CORBA or RMI clients

Page 8: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Example: SOAP RequestExample: SOAP RequestPOST /InStock HTTP/1.1Host: www.stock.orggContent-Type: application/soap+xml;charset=utf-8Content-Length: nnn

<?xml version="1 0"?><?xml version= 1.0 ?><soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"><soap:Body xmlns:m="http://www stock org/stock"><soap:Body xmlns:m= http://www.stock.org/stock >

<m:GetStockPrice><m:StockName>IBM</m:StockName> / G S</m:GetStockPrice>

</soap:Body></soap:Envelope>p p

Page 9: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Example: SOAP ResponseHTTP/1.1 200 OKContent-Type: application/soap; charset=utf-8Content-Length: nnn

Example: SOAP Response

<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"><soap:Body xmlns:m="http://www.stock.org/stock">

<m:GetStockPriceResponse><m:Price>34 5</m:Price><m:Price>34.5</m:Price>

</m:GetStockPriceResponse></soap:Body>

/ E l</soap:Envelope>

Page 10: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Web ServicesWeb ServicesMany people think that web services will soon become ubiquitousubiquitous

Mostly, these people are betting that Microsoft’s support will be enough to convince everyone to switch their middleware

Whether or not they become ubiquitous eventually theyWhether or not they become ubiquitous, eventually they will cooperate with CORBA & EJBs

This will mean that they can be used in any applicationM J / d l HATE XML d W bMany Java gurus/ developers HATE XML and Web services.

too slow, too high an overhead, and “back to the future” because RPC i d d l bj b dRPC is used and real objects can not be passed.

Page 11: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Web Service MiddlewareWeb Service Middleware

Most web service middleware prevents you from p yhaving to know the SOAP language

However, it is a good idea to know something about the language if you intend to use SOAPthe language if you intend to use SOAP

There are several middleware choices if you plan to use web services (and they are roughly compatible):compatible):

.NET Web ServicesJAX-RPCApache Axis

Each of these technologies are configured differently, but the main idea is the samedifferently, but the main idea is the same

Page 12: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

Web Service Architecture

Page 13: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

JAX-WS: OverviewJAX WS: Overview

Page 14: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

JAX WS A t tiJAX-WS Annotations• Annotations play a critical role in JAX-WSAnnotations play a critical role in JAX WS

2.01. Annotations are used in mapping Java to

WSDL d hWSDL and schema2. Annotations are used in runtime to control how

the JAX-WS runtime processes and responds to web service invocations

• Annotations utilized by JAX-WS 2.0 are defined in separate JSRs:• JSR 181: Web Services Metadata for the JavaTM Platform• JSR 222: JavaTM Architecture for XML Binding (JAXB) 2.0• JSR 224: JavaTM API for XML Web Services (JAX-WS) 2.0• JSR 250: Common Annotations for the JavaTM Platform

Page 15: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

W b S i I l t tiWeb Service Implementation1. Write a POJO implementing the servicep g

2. Add @WebService annotation to it

3 Optionally inject a W bS i C t t3. Optionally, inject a WebServiceContext• WebServiceContext makes it possible for a web

service endpoint implementation class to accessservice endpoint implementation class to access message context and security information relative to a request

4. Deploy the application

5 Point your clients at the WSDL5. Point your clients at the WSDL• e.g. http://myserver/myapp/MyService?WSDL

Page 16: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

E lExample: HelloWebService

@WebService(name = "HelloWebService")@SOAPBinding(@SOAPBinding(

style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle =

SOAPBinding.ParameterStyle.WRAPPED)

public class HelloWebService {

@W bM th d@WebMethodpublic String hello(@WebParam(name = "name")

String name){return "Welcome " + name + " !!!";return Welcome + name + !!! ;

}}

Page 17: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

W b S i Cli tWeb Service Clientwsimport http://localhost:8080/calculatorservice/CalculatorService?wsdl

The above command will generate a number of compiled Java classes that allow client applications to access web services:Add.classAddResponse.classCalculator classCalculator.classCalculatorService.classObjectFactory.class

fpackage-info.classSubtract.classSubtractResponse.class

Page 18: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

We will now develop a simple client to access our web serviceWe will now develop a simple client to access our web service.

import javax.xml.ws.WebServiceRef;public class CalculatorServiceClient{ @WebServiceRef(wsdlLocation = "http://localhost:8080/calculatorservice/" + "CalculatorService?wsdl") private static CalculatorService calculatorService; public void calculate() { Calculator calculator = calculatorService.getCalculatorPort(); g ();System.out.println("1 + 2 = " + calculator.add(1, 2)); System.out.println("1 - 2 = " + calculator subtract(1 2));+ calculator.subtract(1, 2)); } public static void main(String[] args) { new CalculatorServiceClient() calculate();new CalculatorServiceClient().calculate(); }}

Page 19: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

W b S i Cli tWeb Service Client

The @WebServiceRef annotation injects an instance of the web service into our client application. Its wsdlLocation attribute contains the URL of the WSDL corresponding to the web service we are invoking.g

Page 20: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

W b S i Cli tWeb Service ClientThe web service class is an instance of a class calledCalculatorService. This class was created when we invoked thewsimport utility; wsimport always generates a class whose name isthe name of the class we implemented plus the "Service" suffix. Weuse this service class to obtain an instance of the web service classwe developed. In our example, we do this by invoking thegetCalculatorPort() method on the CalculatorService instance. Ingeneral, the method to invoke to get an instance of our web serviceclass follows the pattern getNamePort(), where Name is the name ofthe class we wrote to implement the web service Once we get anthe class we wrote to implement the web service. Once we get aninstance of our web service class, we can simply invoke its methodsas with any regular Java object.

Page 21: Web ServicesWeb Services - wmich.edualfuqaha/Fall10/CS5560/lectures/...Web ServicesWeb Services Web services are a middleware, like CORBA and RMI. What makes web services unique is

ReferenceThe J2EE 1.4 Tutorial: Building Web Services with JAX-RPC, Sun MicroSystems. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXRPC.htmly p j j


Recommended