Ntg web services

Preview:

Citation preview

Web ServicesFarag Zakaria

ITI-JAVA 30

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

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).

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

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.)

Example

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

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

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

Web services architecture

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

SOAP messaging

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.

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

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.

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

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

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)

WSDL document parts(cont.)

WSDL document parts(cont.)

WSDL document parts(Data Definition)

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

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

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

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; }}

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.

Example(Client side)

•CalculatorService service = new CalculatorServiceLocator();

AddNumbers calc = service.getCalculatorPort();

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

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

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

JAX-WS Demo

RESTful web services

Agenda

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

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.

Agenda

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

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).

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

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>

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>"; }

}

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

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>

Agenda

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

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.

Agenda

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

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).

RESTful Demo

Any Questions!??

If time allowed

•SOAP Handler Demo

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