Transcript
Page 1: Java Web Services [4/5]: Java API for XML Web Services

Assoc.Prof. Dr. Thanachart Numnondawww.imcinstitute.com

August 2010

Topic 4

Java API for XML Web Services

Page 2: Java Web Services [4/5]: Java API for XML Web Services

2

Agenda

Java APIs for Web Services

JAX-RPC

JAX-WS

Page 3: Java Web Services [4/5]: Java API for XML Web Services

3

Java APIs for Web Services

Page 4: Java Web Services [4/5]: Java API for XML Web Services

4

JWSDP Java Web Services Development Pack (JWSDP) is a free

software development kit (SDK) for developing Web Services

JWSDP has been replaced by GlassFish and WSIT and several components are in Java SE 6.

JWSDP APIs Java API for XML Processing (JAXP), v 1.3

Java Architecture for XML Binding (JAXB), v 1.0 and 2.0

JAX-RPC v 1.1

JAX-WS v 2.0

SAAJ (SOAP with Attachments API for Java)

Java API for XML Registries (JAXR)

Page 5: Java Web Services [4/5]: Java API for XML Web Services

5

Java API for XML Processing (JAXP)

• JAXP enables applications to parse, validate and transform XML documents.

• Java developers can invoke • SAX parser• DOM parser

• The main JAXP APIs are defined in the javax.xml.parsers package• SAXParserFactory• DocumentBuilderFactory

Page 6: Java Web Services [4/5]: Java API for XML Web Services

6

Java API for XML Binding (JAXB)

• JAXB allows Java developers to access and process XML data without having to know XML or XML processing.

• JAXB is a Java technology that enables you to generate Java classes from XML schemas by means of a JAXB binding compiler.

• unmarshalling • XML instance documents into Java content trees

• marshalling • Java content trees back into XML instance documents.

Page 7: Java Web Services [4/5]: Java API for XML Web Services

7

SAAJ (SOAP with AttachmentsAPI for Java™)

• SAAJ provides a convenient API for constructing

and sending SOAP messages without having to

directly create the XML yourself.

• SAAJ was originally part of the Java API

for XML Messaging (JAXM) Separated out from

JAXM 1.0 into JAXM 1.1 and SAAJ 1.1

(SAAJ 1.3 (Java EE 5))

Page 8: Java Web Services [4/5]: Java API for XML Web Services

8

JAXR

• Standard Java API for performing registry operations over diverse set of registries

� Web service publication & discovery

• A unified information model for describing business registry content

• Provides multi-layered API abstractions

� Level 0: for UDDI

� Level 1: for ebXML registry/repository

Page 9: Java Web Services [4/5]: Java API for XML Web Services

9

JAX-RPC

• XML data types to/from Java types mapping

• SOAP Message Handler framework

• WSDL to/from Java mapping

• Servlet-based Web service endpoint model

• JAX-RPC Client Programming Models

• Extensible type mapping

Page 10: Java Web Services [4/5]: Java API for XML Web Services

10

JAX-WS (JAX-RPC 2.0)

• New handler framework

• Metadata-based Web services

• Operational style improvement

• Asynchronous Web services

• Support for non-HTTP transport

• Interoperable attachments (as part of WS-I)

• Secure Web services (as part of Security)

Page 11: Java Web Services [4/5]: Java API for XML Web Services

11

JAXM

• JAXM is not part of Java EE 5

• JAXM-based asynchronous messaging on Java EE 5 is supported via

� Message-driven Bean (MDB) in EJB 3 can receive both JMS and JAXM message types

� Connector 1.5 allows pluggability of various types message providers (JAXM provider and JMS provider)

Page 12: Java Web Services [4/5]: Java API for XML Web Services

12

Java APIs for Web Services Protocols

SOAP JAXM 1.0 (JSR 67), SAAJ

JAX-RPC => JAX-WS 2.0 (JSR 224)

WSDL JAX-RPC => JAX-WS 2.0 (JSR 224)

UDDI JAXR 1.0 (JSR 93)

Page 13: Java Web Services [4/5]: Java API for XML Web Services

13

Sun’s Web Services Stack

Page 14: Java Web Services [4/5]: Java API for XML Web Services

14

JAX-RPC

Page 15: Java Web Services [4/5]: Java API for XML Web Services

Remote Procedure Call (RPC)

RPC, COM, CORBA, RMI:

Synchronous communication: calling process blocks until there is a response

More tightly coupled (than non-RPC model): client must find recipients and know method and its arguments

Non persistent

Client System Server System

Request

Response

Page 16: Java Web Services [4/5]: Java API for XML Web Services

Remote Procedure Calls

Protocol Run TimeRun Time

ServantClient

Stub Skeleton

Interface Interface

Common Interface between client and Server Stub for client, Skeleton/Tie for server On-the-wire protocol needs to be agreed upon

Page 17: Java Web Services [4/5]: Java API for XML Web Services

RPC Example – Java RMI <<Interface>>Java Remote

Interface

Java Remote Method Protocol (JRMP) isOn-the-wire protocol

Remote ObjectImplementation

RMI ServerSkeleton

RMI ClientStub

rmic compiler

Client

Page 18: Java Web Services [4/5]: Java API for XML Web Services

RPC Example - JAX-RPC

Page 19: Java Web Services [4/5]: Java API for XML Web Services

Common Interface Service is described in IDL (Interface Description

Language) IDL of CORBA service Java RMI interface in RMI (Language specific) WSDL for Web service

Used by tools to statically or dynamically generate and/or configure interfaces, proxies, and ties in a network technology specific environment

Page 20: Java Web Services [4/5]: Java API for XML Web Services

What is JAX-RPC?

Java™ API for XML-based RPC Web Services operations are performed by exchanging

SOAP 1.1 messages

Services are described using WSDL• WSDL is the contract between service provider and

client (only contract needed)

Web service endpoints and clients use JAX-RPC programming model

Key technology for Web Services in the J2EE 1.4 platform

Page 21: Java Web Services [4/5]: Java API for XML Web Services

JAX-RPC Design Goals

Easy to use programming model• For both defining & using a service

Hides all the plumbing• You don't have to create SOAP messages yourself

SOAP and WSDL-based interoperability• Interoperate with any SOAP 1.1 compliant peers

Extensibility and Modularity• Support new versions of XML specification, i.e. SOAP 1.2

and beyond• Message handler architecture

Page 22: Java Web Services [4/5]: Java API for XML Web Services

JAX-RPC Architecture

Server-side JAX-RPCRuntime System

Container

JAX-RPCService Endpoint

WSDLJava

WSDL Document

HTTP

Client-side JAX-RPCRuntime System

SOAP

Container

JavaWSDLGenerated Code

JAX-RPC Client

Page 23: Java Web Services [4/5]: Java API for XML Web Services

JAX-RPC Runtime System

Core of a JAX-RPC implementation:• Library that provides runtime services

for JAX-RPC mechanisms• Implements some of the JAX-RPC APIs

Client side:• Can be implemented over J2SE™, J2EE™ or J2ME™

platforms

Server-side:• J2EE 1.3 or 1.4 Containers: EJB™ or Servlet

Page 24: Java Web Services [4/5]: Java API for XML Web Services

Developing a Web Service

tool

Interfaces (java.rmi.Remote type)Must follow JAX-RPC conventions

Implementation classesServlet-based endpoint modelOptional handler and serializer classes

Packaged application (war file)

WSDL Service contract

Page 25: Java Web Services [4/5]: Java API for XML Web Services

JAX-RPC : Web Service Endpoint

HTTP

Server-Side JAX-RPCRuntime System

SOAP

Container

JAX-RPC Service Endpoint

WSDL<-->Java

WSDL Document

Page 26: Java Web Services [4/5]: Java API for XML Web Services

26

JAX-WS

Page 27: Java Web Services [4/5]: Java API for XML Web Services

27

Quick overview of JAX-WS

Easy way to use Java API for XML Web Services Replace JAX-RPC

Just add @annotation to POJO XML Descriptor free programming

Layer architecture SOAP 1.2 (Document/Literal) Use JAXB for data binding Protocol and transport independence Part of Java SE6 and Java EE 5 Platform

Page 28: Java Web Services [4/5]: Java API for XML Web Services

28

JAX-WS Layered Architecture

Page 29: Java Web Services [4/5]: Java API for XML Web Services

29

What Does It Mean?

• Upper layer uses annotations extensively• Easy to use

• Great toolability

• Fewer generated classes

• Lower layer is more traditional• API-based

• For advanced scenarios

• Most application will use the upper layer only

• Either way, portability is guaranteed

Page 30: Java Web Services [4/5]: Java API for XML Web Services

30

Two ways to create a Web Service

• Starting from a WSDL file> Generate classes using wsimport (=> xjc)

> WS interface> WS implementation skeleton class

> Implement WS interface> Build and deploy

• Starting from a Java class (POJO) > Annotate POJO > Build and deploy

> WSDL file generated automatically

Page 31: Java Web Services [4/5]: Java API for XML Web Services

31

Start with a Java Class

Page 32: Java Web Services [4/5]: Java API for XML Web Services

32

Server-Side Programming Model: POJO

1 Write a POJO implementing the service

2 Add @WebService annotation to it

3 Build and Deploy the applicationWSDL is automatically generated at runtime

1 Point your clients at the WSDLe.g. http://myserver/myapp/MyService?WSDL

Page 33: Java Web Services [4/5]: Java API for XML Web Services

33

Example 1: Servlet-Based Endpoint

@WebServicepublic 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

Page 34: Java Web Services [4/5]: Java API for XML Web Services

34

Service Description Default Mapping• Java mapping => WSDL

Page 35: Java Web Services [4/5]: Java API for XML Web Services

35

Example 2: EJB-Based Endpoint

@WebService@Statelesspublic class Calculator { 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...

Page 36: Java Web Services [4/5]: Java API for XML Web Services

36

Customizing 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 ...}

}

Page 37: Java Web Services [4/5]: Java API for XML Web Services

37

Start with a WSDL file

Page 38: Java Web Services [4/5]: Java API for XML Web Services

38

Generating an interface from WSDL• WSDL => Java generation

Page 39: Java Web Services [4/5]: Java API for XML Web Services

39

Implementing a Web Service froma Generated Interface

Page 40: Java Web Services [4/5]: Java API for XML Web Services

40

Server Side Web Service

Page 41: Java Web Services [4/5]: Java API for XML Web Services

41

JAX-WS uses JAXB for Data Binding

Page 42: Java Web Services [4/5]: Java API for XML Web Services

42

add() Parameter : Example

Page 43: Java Web Services [4/5]: Java API for XML Web Services

43

JAXB XML Schema to Java Mapping

Page 44: Java Web Services [4/5]: Java API for XML Web Services

44

Client Side Programming

Page 45: Java Web Services [4/5]: Java API for XML Web Services

45

Java SE Client Side Programming

• Point a tool (NetBeans or wsimport) at the WSDL for the service

wsimport http://example.org/Calculator.wsdl

• Generate annotated classes and interfaces

• Call new on the service class

• Get a proxy using a get<ServiceName>Port method

• Invoke any remote operations

Page 46: Java Web Services [4/5]: Java API for XML Web Services

46

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

Page 47: Java Web Services [4/5]: Java API for XML Web Services

47

Java EE Client Side Programming

• Point a tool (NetBeans or wsimport) at the WSDL for the service

wsimport http://example.org/calculator.wsdl

• Generate annotated classes and interfaces

• Inject a @WebServiceReference of theappropriate type

• No JNDI needed

• Invoke any remote operations

Page 48: Java Web Services [4/5]: Java API for XML Web Services

48

Example : Java EE-Based Client

@Statelesspublic class MyBean { // Resource injection

@WebServiceRef(CalculatorService.class) Calculator proxy;

public int mymethod() {return proxy.add(35, 7);

}

Page 49: Java Web Services [4/5]: Java API for XML Web Services

49

Client Side

Page 50: Java Web Services [4/5]: Java API for XML Web Services

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

Page 51: Java Web Services [4/5]: Java API for XML Web Services

@WebService• Marks a Java class as implementing a

Web Service, or a Java interface as defining a Web Service interface.

• Attributes> endpointInterface> name> portName> serviceName> targetNamespace> wsdlLocation

Page 52: Java Web Services [4/5]: Java API for XML Web Services

@WebMethod

• Customizes a method that is exposed as a Web Service operation

• The method is not required to throw java.rmi.RemoteException.

• Attributes> action> exclude> operationName

Page 53: Java Web Services [4/5]: Java API for XML Web Services

@WebParam

• Customizes the mapping of an individual parameter to a Web Service message part and XML element.

• Attributes> header> mode> name> partName> targetNamespace

Page 54: Java Web Services [4/5]: Java API for XML Web Services

@WebResult

• Customizes the mapping of the return value to a WSDL part and XML element.

• Attributes> header> name> partName> targetNamespace

Page 55: Java Web Services [4/5]: Java API for XML Web Services

JAX-WS - Provider

• Web Service endpoints may choose to work at the XML message level by implementing the Provider interface.

• The endpoint accesses the message or message payload using this low-level, generic API

• Implement one of the following> Provider<Source>> Provider<SOAPMessage>> Provider<DataSource>

Page 56: Java Web Services [4/5]: Java API for XML Web Services

JAX-WS - Provider

Page 57: Java Web Services [4/5]: Java API for XML Web Services

JAX-WS - Dispatch

• Web service client applications may choose to work at the XML message level by using the Dispatch<T> APIs.

• The javax.xml.ws.Dispatch<T> interface provides support for the dynamic invocation of service endpoint operations.

• Similar to Provider on server side

Page 58: Java Web Services [4/5]: Java API for XML Web Services

JAX-WS - Dispatch

Page 59: Java Web Services [4/5]: Java API for XML Web Services

59

Resources

Some contents are borrowed from the presentation slides of Sang Shin, Java™ Technology Evangelist, Sun Microsystems, Inc.

Web Services and Java, Elsa Estevez, Tomasz Janowski and Gabriel Oteniya, UNU-IIST, Macau

Page 60: Java Web Services [4/5]: Java API for XML Web Services

60

Thank you

[email protected]

www.facebook.com/imcinstitute

www.imcinstitute.com