ntg web services

48
Web Services Farag Zakaria ITI-JAVA 30

Upload: farag-zakaria

Post on 11-May-2015

448 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Ntg   web services

Web ServicesFarag Zakaria

ITI-JAVA 30

Page 2: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 3: Ntg   web services

Web services concept

•Web service is a software system identified by a URI whose public interfaces and bindings are defined ,described and discovered by XML artifacts.(W3C)

•Client server application•Application to application communication•Interoperability (standard

communication) •Neutral (platform & language

independent).

Page 4: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 5: Ntg   web services

Why web services

•Interoperability (SOAP messaging).•Economical (recycled components, no

installation and tight integration of software).

•Accessible (applications are exposed and accessed on the web.)

•Available (services are on any device, any where, anytime.)

•Scalable (no limit on scope of application and their number.)

Page 6: Ntg   web services

Example

Page 7: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 8: Ntg   web services

When to use web services? •Applications do not have severe

restrictions on reliability and speed.•Cooperation among different application.•Upgrading services independently from

clients without changing the WSDL.•Services can be easily expressed with

simple request/response semantics and simple state

Page 9: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 10: Ntg   web services

Web services architecture

Page 11: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 12: Ntg   web services

SOAP messaging

Page 13: Ntg   web services

SOAP(Simple Object Access Protocol)

•Define communication format between client and web service(XML message format).

•Wire protocol extension for conveying RPC calls.

•SOAP is not a transport protocol. You must attach your message to a transport mechanism like HTTP.

Page 14: Ntg   web services

SOAP structure• A SOAP message is contained in an envelop.• The envelop element in turn contain (in order)

1. An optional header with one or more child entries. (security , transaction) 2. A body element that can contain one or more child entries. These child entries may contain arbitrary XML data.(application data)

• Header entries may optionally have a “mustUnderstand” attribute. mustUnderstand=1 recipient must process header mustUnderstand=0 optional header

Page 15: Ntg   web services

SOAP structure(cont.)

•The body contains the XML message that you are transmitting.

• The message format is not specified by SOAP.The <Body></Body> tag pairs are just a way to notify the recipient that the actual XML message is contained therein.The recipient decides what to do with the message.

Page 16: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL• JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 17: Ntg   web services

WSDL(Web Service Description Language)•Defines what your service does and how it

is invoked.•Guideline for constructing SOAP message.•XML language for writing

APIs(generating client code to use the web service).

•Verbose

Page 18: Ntg   web services

WSDL documents parts

•Data definition (in XML) for custom types.•Abstract message definitions (request,

response)•Organization of messages into “ports” and

“operations” (classes and methods).•Protocol bindings (to SOAP, for example)•Service point locations (URLs)

Page 19: Ntg   web services

WSDL document parts(cont.)

Page 20: Ntg   web services

WSDL document parts(cont.)

Page 21: Ntg   web services

WSDL document parts(Data Definition)

Page 22: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL•JAX-WS• JAX-WS vs. JAX-RPC•RESTful web services

Page 23: Ntg   web services

JAX-WS

•Simpler way to develop/deploy web services- POJO + Annotation.- No deployment descriptor is needed.- Layered programming model

•Layered programming model- Server side- Client side- Business layer

Page 24: Ntg   web services

Programming model(server side)•Write a POJO implementing the service•Add @WebService annotation to it•Deploy the application•Point your clients at the WSDL

http://mysite/myapp/service.wsdl

Page 25: Ntg   web services

Example(Server side)

@WebService(name="AddNumbers", portName="CalculatorPort", serviceName="CalculatorService")public class NumbersWebService{ @WebMethod(operationName="add") @WebResult(name="result") public int add(@WebParam(name="first") int first,

@WebParam(name="second") int second) { return first + second; }}

Page 26: Ntg   web services

Programming model(client side)•You need to know the url of WSDL•Generate service classes and interfaces.•Create object from service class•Get a proxy using a

get<ServiceName>Port method.•Invoke any remote operations.

Page 27: Ntg   web services

Example(Client side)

•CalculatorService service = new CalculatorServiceLocator();

AddNumbers calc = service.getCalculatorPort();

System.out.println(calc.add(3, 4));

Page 28: Ntg   web services

Agenda

•Web services concept.•Why web services?•When to use web services?•Web services architecture•SOAP •WSDL• JAX-WS•JAX-WS vs. JAX-RPC•RESTful web services

Page 29: Ntg   web services

JAX-WS vs. JAX-RPCJAX-RPC JAX-WS

Map to JAVA 1.4 Map to JAVA 1.5 and higher

Support SOAP 1.1 Support SOAP 1.1 and SOAP 1.2

Has its own data mapping model Data mapping model (JAXB)

Support MTOM through (JAXB)

Asynchronous communication

Page 30: Ntg   web services

JAX-WS Demo

Page 31: Ntg   web services

RESTful web services

Page 32: Ntg   web services

Agenda

•REST Architecture principales •RESTful web services & WADL.•REST vs. Traditional web services•RESTful advantages

Page 33: Ntg   web services

REST Architecture principales

•Representational State Transfer.•Every thing is a resource.•Resources are addressable.•Resources have an interface(operations

and data types).•Protocol is client-server, stateless,

cacheable, layered.

Page 34: Ntg   web services

Agenda

•REST Architecture principales •RESTful web services & WADL.•REST vs. Traditional web services•RESTful advantages

Page 35: Ntg   web services

RESTful web services & WADL.

•REST applied to web services.- web service accessible through URI.- operations are HTTP primitives(PUT,GET,..).- Web services returns a MIME type(XML, JSON, …).

•More resource efficient than SOAP•RESTful web services

- java class + annotations (for URI, operations, Data types).

Page 36: Ntg   web services

RESTful web services & WADL.(cont.)

•Annotations- @Path(“”) for URI of service.- @GET,@POST,.. for Http methods.- @Produces(“…”) to define format of returned data- @Consumes (“…”) to define format of accepted input data

Page 37: Ntg   web services

RESTful web services & WADL.(cont.)

<servlet> <servlet-name>ServletAdaptor</servlet-name> <servlet-class>

com.sun.jersey.spi.container.servlet.ServletContainer

</servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/resources/*</url-pattern></servlet-mapping>

Page 38: Ntg   web services

RESTful web services & WADL.(cont.)

@Path("/hello")

@Consumes("text/html")

public class RESTWSTest{ @GET @Produces("text/html") public String sayHello(@QueryParam("name") String name) { return "<html><body><h1>Hello " + name +

"</h1></body></html>"; }

}

Page 39: Ntg   web services

RESTful web services & WADL.(cont.)

• WADL web application description language.- easy to understand.- HTTP assumed.

• XML-based language.• Development language + platform neutral.• Aligned with REST terminology.• WADL elements

- resources- methods- representations

Page 40: Ntg   web services

WADL<application><doc jersey:generatedBy="Jersey: 1.1.4 11/10/2009 05:36

PM"/><resources

base="http://localhost:8080/WebServicesREST/resources/"><resource path="/hello"> <method id="sayHello" name="GET"> <request> <param name="name" style="query" type="xs:string"/> </request> <response> <representation mediaType="text/html"/> </response> </method></resource></resources></application>

Page 41: Ntg   web services

Agenda

•REST Architecture principales.•RESTful web services & WADL.•REST vs. Traditional web services•RESTful advantages

Page 42: Ntg   web services

REST vs. Traditional web services

•Traditional web services- a lot of custom methods(ports according to business).- use http to transport soap

•RESTful- fixed methods(Http methods).- Http is the protocol.

Page 43: Ntg   web services

Agenda

•REST Architecture principales •RESTful web services & WADL.•REST vs. Traditional web services•RESTful advantages

Page 44: Ntg   web services

RESTful advantages

•More secure over http (use web port 80)•Ease of testing (only browser needed).•Thin client.•Save memory.(no need to build xml tree in memory).

Page 45: Ntg   web services

RESTful Demo

Page 46: Ntg   web services

Any Questions!??

Page 47: Ntg   web services

If time allowed

•SOAP Handler Demo

Page 48: Ntg   web services

References •JavaPassion web site•Sun Java JEE6 tutorial•Sun Java Web Services tutorial