34
JAX-WS Basics JAX-WS Basics

JAX-WS Basics - Lanzhou University

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: JAX-WS Basics - Lanzhou University

JAX-WS BasicsJAX-WS Basics

Page 2: JAX-WS Basics - Lanzhou University

2

• Quick overview of JAX-WS> Differences from JAX-RPC

• JAX-WS programming Model> Layered programming model> Server side> Client side

Agenda

Page 3: JAX-WS Basics - Lanzhou University

3

Quick Overview of JAX-WS 2.0• 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.0• Protocol and transport independence

Page 4: JAX-WS Basics - Lanzhou University

Layered ProgrammingLayered ProgrammingModelModel

Page 5: JAX-WS Basics - Lanzhou University

5

Layered Programming Model

Calls Into

Implemented on Top of

Messaging Layer:Dispatch/Provider

Application Code

Strongly-Typed Layer:Annotated Classes

Page 6: JAX-WS Basics - Lanzhou University

6

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 7: JAX-WS Basics - Lanzhou University

Programming ModelProgramming Modelat the Server Sideat the Server Side

Page 8: JAX-WS Basics - Lanzhou University

8

Two ways to create a Web Service• Starting from a WSDL file (top-down approach)> Generate classes using ws i mpor t>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

Page 9: JAX-WS Basics - Lanzhou University

9

Server-Side Programming Model:(Starting from POJO)

1 Write a POJO implementing the service2 Add @WebService annotation to it3 Optionally, inject a WebServiceContext4 Deploy the application5 Point your clients at the WSDL> e.g. http://myserver/myapp/MyService?WSDL

Page 10: JAX-WS Basics - Lanzhou University

10

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 11: JAX-WS Basics - Lanzhou University

11

Example 2: EJB-Based Endpoint@WebService@Statelesspublic 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...

Page 12: JAX-WS Basics - Lanzhou University

12

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 13: JAX-WS Basics - Lanzhou University

13

Demo• Build a “Hello World” Web service using

@WebService annotation• Test the Web service• Display the generated WSDL document

Page 14: JAX-WS Basics - Lanzhou University

Client SideClient SideProgramming:Programming:Java SE & Java EEJava SE & Java EE

Page 15: JAX-WS Basics - Lanzhou University

15

Java SE Client-Side Programming1. Point a tool (NetBeans or wsimport) at the WSDL

for the servicewsimport http://example.org/calculator.wsdl

2. Generate annotated classes and interfaces3. Call new on the service class4. Get a proxy using a get<ServiceName>Port method5. Invoke any remote operations

Page 16: JAX-WS Basics - Lanzhou University

16

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 17: JAX-WS Basics - Lanzhou University

17

Demo• Build and run a Web service client of “Hello World”

Web service using the WSDL document

Page 18: JAX-WS Basics - Lanzhou University

18

Java EE Client-Side Programming1. Point a tool (NetBeans or wsimport) at the WSDL for

the servicewsimport http://example.org/calculator.wsdl

2. Generate annotated classes and interfaces3. Inject a @WebServiceReference of the

appropriate type• No JNDI needed

4. Invoke any remote operations

Page 19: JAX-WS Basics - Lanzhou University

19

Example: Java EE-Based Client

@Statelesspublic class MyBean {

// Resource injection@WebServiceRef(CalculatorService.class)

Calculator proxy;

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

}

Page 20: JAX-WS Basics - Lanzhou University

AnnotationsAnnotations

Page 21: JAX-WS Basics - Lanzhou University

21

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 22: JAX-WS Basics - Lanzhou University

22

@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 23: JAX-WS Basics - Lanzhou University

23

@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 24: JAX-WS Basics - Lanzhou University

24

@WebParam• Customizes the mapping of an individual parameter

to a Web Service message part and XML element.• Attributes> header> mode> name> partName> targetNamespace

Page 25: JAX-WS Basics - Lanzhou University

25

@WebResult• Customizes the mapping of the return value to a

WSDL part and XML element.• Attributes> header> name> partName> targetNamespace

Page 26: JAX-WS Basics - Lanzhou University

26

Example

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

}

Page 27: JAX-WS Basics - Lanzhou University

Protocol and TransportProtocol and TransportIndependenceIndependence

Page 28: JAX-WS Basics - Lanzhou University

28

Protocol 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

Page 29: JAX-WS Basics - Lanzhou University

29

Example

@WebService@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)public class AddNumbersImpl {

// More code

Page 30: JAX-WS Basics - Lanzhou University

HandlerHandler

Page 31: JAX-WS Basics - Lanzhou University

31

Handler 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

Page 32: JAX-WS Basics - Lanzhou University

32

Logical Handler

public class MyLogicalHandler implements LogicalHandler<LogicalMessageContext> { public boolean handleMessage(LogicalMessageContext messageContext) { LogicalMessage msg = messageContext.getMessage(); return true; } // other methods}

Page 33: JAX-WS Basics - Lanzhou University

33

SOAP Handler

public class MySOAPHandler implements SOAPHandler<SOAPMessageContext> {

public boolean handleMessage(SOAPMessageContext messageContext) { SOAPMessage msg = messageContext.getMessage(); return true; } // other methods}

Page 34: JAX-WS Basics - Lanzhou University

JAX-WS BasicsJAX-WS Basics