Você está na página 1de 13

Berner Fachhochschule

Technik und Informatik

JAX-WS
Java API for XML-Based
Web Services
Prof. Dr. Eric Dubuis
Berner Fachhochschule
Biel
Overview

● The motivation for JAX-WS


● Architecture of JAX-WS and WSDL ↔ JAX-WS
● Java to WSDL by example
● Developing a web service
● Developing a web service client

Note: These slide are about a simple and short introduction on


JAX-WS and, therefore, by no means complete.

01/29/08 JAX-WS 2
Why JAX-WS?

Given web services, WSDL, XML, and SOAP, JAX-WS defines ...

● how to program service endpoints in Java


● how to program web service clients
● how to program tools that generated otherwise missing
intermediaries
● how to customize the sending and receiving of SOAP
messages

when using Java.

JAX-WS is an industry standard. Its specification can be found


here: http://jcp.org/en/jsr/detail?id=224

JAX-WS defines an user API and a service provider API. We look


at the user API only.
01/29/08 JAX-WS 3
Some Goals Being Achieved by JAX-WS

● Standard mapping of WSDL to Java


● Standard mapping of Java to WSDL (!)
● Standard rules for creating artifacts such as stubs and
skeletons
● Standard rules for so-called handlers
● Standard way of using JAXB, another standard, which defines
the Java – XML binding
● Standard way of introducing security into web services (when
using Java)
● ...

01/29/08 JAX-WS 4
JAX-WS Architecture

Client Service
Application Implementation
Stub / Proxy Tie
JAX-WS JAX-WS
Runtime Runtime
SOAP messages
HTTP transport

● The client invokes a stub method which is delegated to the


JAX-WS runtime in order to send an appropriate SOAP
message to the server
● On the server side, the tie converts the received message back
into a method call on the actual service implementation
● The stub and tie classes are dynamically generated by the
provider platform

01/29/08 JAX-WS 5
Relationship between WSDL and JAX-WS

WSDL

«use» Service Service


Client
Interface Implementation

● JAX-WS can generate a WSDL document from the Java


implementation of a web service (code-first)
● JAX-WS can import a WSDL document to generate the Java
code required to implement the web service (contract-first) or a
web service client

01/29/08 JAX-WS 6
Java to WSDL Mapping

● A class annotated with @WebService is mapped to <portType>


element
● Public Java methods are mapped to <operation> elements with
corresponding <input> and <output> messages
● Java exceptions are mapped to <fault> elements

@WebService
public class StockQuoter {
public double getQuote(String symbol) throws NotFoundException {
return 123.45;
}
}

<portType name="StockQuoter">
<operation name="getQuote" parameterOrder="symbol">
<input message="tns:getQuote"/>
<output message="tns:getQuoteResponse"/>
<fault name="NotFoundException"
message="tns:NotFoundException"/>
</operation>
</portType>
01/29/08 JAX-WS 7
Developing a Web Service
Contract-first approach versus code-first
approach
A good way to get initiated into JAX-WS is to first develop a Web
service. You can develop a Web service using one of two
approaches:

● Contract first: Start with a WSDL contract, and generate a


Java class to implement the service.
● Code first: Start with a Java class, and use annotations to
generate both a WSDL file and a Java interface.

The contract-first WSDL approach requires a good understanding


of WSDL and XSD (XML Schema Definition) for defining
message formats. It's a good idea to start with the code-first
approach if you're fairly new to Web services, which is what
you'll use in this introduction to develop Web services.

01/29/08 JAX-WS 8
Developing a Web Service

1. Define the web service endpoint interface and implement that


inteface – or –
define a @WebService-annotated class
2. Compile the web service
3. Generate the portable artifacts required to deploy the service
4. Package the service code and the generated files into a web
archive
5. Deploy the web archive into a running web container
(additional artifacts like the WSDL document are generated)

«webservice»
NotFoundException
StockQuoter

01/29/08 JAX-WS 9
Developing a Web Service

The @WebService-Annotated Class


● The class must be annotated with the @WebService
annotation, and its method may be annotated with the
@WebMethod annotation.
● The methods must have JAXB compatible parameters and
return types [JAX_WS, 3.6].

@WebService(name = “StockQuoter”,
targetNamespace = “http://stocks.org”) // properties: optional
public class StockQuoter {
@WebMethod // optional
public double getQuote(String symbol)
throws NotFoundException {
...
return ...;
}
}
01/29/08 JAX-WS 10
Developing a Web Service Client

1. Generate the artifacts needed to connect to the web service.


2. Implement and compile the client application.
3. Run the client.

javax.xml.ws
Service
{abstract}

«use»
StockQuoter
Service

«create»
«use»
StockQuoter «interface»
NotFoundException
Client StockQuoter

01/29/08 JAX-WS 11
Developing a Web Service Client

The Client Implementation


● The client creates a Service object, obtains a stub from it, and
then simply invokes its methods.

public class StockQuoterClient {


public static void main(String[] args) throws Exception {
StockQuoterService service = new StockQuoterService();
StockQuoter port = service.getStockQuoterPort();
double quote = port.getQuote(“IBM”);
...
}
}

01/29/08 JAX-WS 12
Web Service Client Using Alternate Address

● You can tell the client to use an alternate address


● You may use this feature to introduce a monitor for the traffic
● You may us Tcpmon (https://tcpmon.dev.java.net/)

public class StockQuoterClient {


public static void main(String[] args) throws Exception {
StockQuoterService service = new StockQuoterService();
StockQuoter port = service.getStockQuoterPort();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext()
.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:8090/StockQuoter/StockQuoterService");
double quote = port.getQuote(“IBM”);
...
}
}
01/29/08 JAX-WS 13

Você também pode gostar