JavaTM API for XML Web Services
User's Guide

Specification Version: 2.0
Reference Implementation (RI) Version: 2.0 ea3

Last modified: 12/14/2005

Contents

  1. JAX-WS 2.0 User's Guide
    1. Contents
    2. 1. Introduction
      1. 1.1 What's new in JAX-WS 2.0 ea3
        1. 1.1.1 new JAXWS 2.0 API
        2. 1.1.2 Fully Dynamic Runtime
        3. 1.1.3 MTOM & Swaref
        4. 1.1.4 SOAP 1.2
      2. 2.1 Features introduced in this release
        1. 2.1.1 JAXB 2.0
        2. 2.1.2 Annotations
        3. 2.1.3 Customizations
        4. 2.1.4 Improved Handler Framework
        5. 2.1.5 Provider
        6. 2.1.6 Dispatch
        7. 2.1.7 Asynchronous Client Operations
    3. 3. Programming Model
      1. 3.1 Server
        1. 3.1.1 Starting from Java
        2. 3.1.1.1 Generate Portable Artifacts
        3. 3.1.1.2 Create a WAR file to deploy
        4. 3.1.2 Starting from a WSDL File
        5. 3.1.2.1 Generate a Service Endpoint Interface
        6. 3.1.2.2 Implement the Service Endpoint Interface
        7. 3.1.2.3 Create a WAR
        8. 3.1.2.4 J2SE Endpoints
      2. 3.2 Client
        1. 3.2.1 Dynamic Proxy
        2. 3.2.1.1 Generate Client Artifacts
        3. 3.2.2 Dispatch
    4. 4. Useful Information

1. Introduction

This document describes the new features available in this release of the Java API for XML Web Services (JAX-WS) 2.0 (JSR 224) Standard Implementation (SI). The main focus of this document is to describe the tools used to develop JAX-WS 2.0 RI web service endpoints and clients. Readers of this document should be familiar with web services XML, XML Schema, WSDL and ideally with JAX-RPC 1.1. Please refer to Section 3. Useful Information for links to more information about web services and JAX-RPC 1.1.

1.1 What's new in JAX-WS 2.0 ea3

1.1 What's new in JAX-WS 2.0 ea3

1.1.1 new JAXWS 2.0 API

JAXRPC 2.0 EA1 was implementation of JAXRPC 2.0 specification. Sometime after JAXRPC 2.0 EA1 release JAXRPC 2.0 specification was renamed to JAXWS 2.0. JAXWS 2.0 defines all new set of packages and removes JAXRPC 1.1 specific APIs.

JAXWS 2.0 SI is the standard implementation of this new set of APIs defined by JAXWS 2.0 specification being developed under JSR224. Because of this new set of APIs, an application written with JAXRPC 2.0 EA1 will not compile. They will need to be refactored and modified as per JAXWS 2.0 API.

For example,

fromwsdl.client.AddNumbersClient.java creates proxy port (there is no generated stub):
AddNumbersPortType port = new AddNumbersService().getAddNumbersPort();

1.1.2 Fully Dynamic Runtime

In this release of JAX-WS, all artifacts generated by apt, wsimport and wsgen are portable.  JAX-WS uses the annotations within the SEI to aid in marshalling/unmarshalling messages.  Because we no longer generated non-portable artifacts, we were able to get rid of wsdeploy.  The user now can create their own deployable WAR file. To learn more about creating a WAR file and the deployment descriptor, click here.

It should also be noted that wscompile has been replaced by two new tools: wsimport and wsgen.  wsimport is used for importing WSDLs and generating the portable artifacts.  wsgen processes a compiled SEI and generates the portable artifacts.  You will also note that wsgen no longer generates the WSDL at tool-time, the wsdl is now generated when the endpoint is deployed.  There however is an option on wsgen to generate the WSDL for developement purposes.

1.1.3 MTOM & swaRef

JAXWS 2.0 brings in support for optimized transmission of binary data as specified by MTOM (SOAP Message Transmission Optimization Mechanism)/XOP (XML Binary Optimized Packagin) and swaRef (SOAP Attachment Refenreces specified by WS-I Attachment Profile 1.0).

  • MTOM allows optimized transmission of binary data - any xs:base64Binary or xs:hexBinary schema type can be send as attachment following rules defined by XOP encoding and MTOM specification.
  • In swaRef, an XML element of wsi:swaRef type (defined by WS-I Attachment Profile 1.0) is send as attachment and a refecrenced based on CID URL schema is put in place of the content of the element.
For details on MTOM and swaRef features refer to JAXWS 2.0 MTOM and swaRef.

1.1.4 SOAP 1.2

SOAP 1.2 support is added to JAXWS 2.0. Currently the support is limited and not tested extensively. For details refer to JAXWS 2.0 SOAP 1.2.

2.1 Features introduced in Early Access 1

JAX-WS 2.0 has a number of significant improvements over JAX-RPC 1.1. Some of these improvements include improved data-binding with JAXB, standard mechanisms for customizing the mapping between WSDL/XML and Java, improved handlers, support for client side asynchronous operations, improved dynamic invocation support via new Dispatch and Provider interfaces/implementations.

2.1.1 JAXB 2.0

The most notable change in JAX-WS 2.0 is the use of JAXB 2.0 for data-binding between Java and XML. JAX-RPC 1.0 specified a limited mapping between XML and Java, this was mainly due to the fact that JAXB was not going to be done in time for JAX-RPC 1.0. With the completion of JAXB, there is no longer a reason to have two separate sets of mapping rules between XML and Java. JAX-WS will also support the full set of JAXB binding customizations and optional schema validation.

2.1.2 Annotations

JAX-WS 2.0 relies heavily on the use of annotations as provided by A Metadata Facility for the Java Programming Language (JSR 175) and and Web Services Metadata for the Java Platform (JSR 181) as well as additional annotations defined by JAX-WS 2.0. These annotations are used to customize the mapping from Java to XML schema/WSDL and are used at runtime to alleviate the need for non-portable serializers/deserializers that were generated in JAX-RPC 1.x.

Another one of the shortcomings of the JAX-RPC 1.x SI, was the inability to map parameter names to part meaningful part/element names in the WSDL file. JSR 181 provides a WebParam annotation that allows the developer to specify what the WSDL part/element name will be.

The JAX-WS SI utilizes a new tool apt (annotation processing tool) that was introduced in J2SDK 5.0. apt allows the SI to process Java source files directly to generate the portable artifacts specified by the JAX-WS 2.0 specification. apt will be covered in more detail in section 3.1.1.

For more information on the annotations used by JAX-WS 2.0 please refer to JAX-WS 2.0 annotations.

2.1.3 Customizations

The JAX-RPC 1.1 specification did not define a standard customization architecture. However JAX-RPC 1.x SI had limited WSDL to Java customization support. It allowed a JAX-RPC 1.x application to:

  • Define a package where Java artifacts mapped from a WSDL file will be generated

  • Package customization for value classes mapped from the imported XML schemas by the WSDL document

  • Handler chain customization

But these customizations were not portable and could not be used across other JAX-RPC implementations.

JAX-WS 2.0 specification defines standard XML based customization for a WSDL file to Java mapping and to control certain features. These customizations, or binding declarations, can customize almost all WSDL components that can be mapped to Java, such as the service endpoint interface class, method name, parameter name, exception class, etc. The other important thing you can do with these binding declarations is to control certain features, such as asynchrony, provider, wrapper style, and additional headers. For example, a client application can enable asynchrony for a particular operation in a portType, all operations in a portType, or all portType operations defined in the WSDL file.

These binding declarations can be inlined in a WSDL file or can live outside as an external file. The binding declarations closely align with the JAXB binding declarations. An application importing a WSDL file can inline JAXB bindings inside JAX-WS binding declarations to customize the inlined schema declared in the WSDL file. Schema files that are imported from a WSDL file can be customized using JAXB binding files and can be passed to wscompile using the -b option switch.

These are the main customization features:

  • Scoped binding declarations. An XPath expression is used to specify the target node of the WSDL file on which customization should be applied.
  • Close alignment with JAXB bindings. JAXB binding declarations can be inlined in an external JAX-WS binding file.
  • Feature Control. Features such as asynchrony, wrapper style, additional header mapping, and provider interfaces can be enabled or disabled.
  • Handler chain customization (not yet specified by the 2.0 specification)

The following WSDL component's mapped Java names can be modified:

  • generated service endpoint interface class
  • method
  • method parameter
  • generated exception class (for WSDL fault and header fault exceptions)
  • header parameter
  • generated service class
  • port accessor methods in the generated service class

XML Schema Java mapping can be customized using standard JAXB customizations.

For more information on the customizations used by JAX-WS 2.0 please refer to JAX-WS 2.0 customizations.

2.1.4 Improved Handler Framework

JAX-WS 2.0 defines two types of handlers: logical and protocol handlers. While protocol handlers have access to an entire message such as a SOAP message, logical handlers deal only with the payload of a message and are independent of the protocol being used. Handler chains can now be configured on a per-port, per-protocol, or per-service basis. A new framework of context objects has been added to allow client code to share information easily with handlers.

For more information on the handler framework in JAX-WS 2.0 please refer to JAX-WS 2.0 Handler Framework.

2.1.5 Provider

Web service endpoints may choose to work at the XML message level by implementing the Provider interface. Here the endpoints access messages or message payloads using this low level, generic API.

For more information on providers in JAX-WS 2.0 please refer to JAX-WS 2.0 Provider.

2.1.6 Dispatch

The Dispatch API is intended for advanced XML developers who prefer to use XML constructs at the java.lang.transform.Source or javax.xml.soap.SOAPMessage level. For added convenience use of the Dispatch API with JAXB data-bound objects is supported. The Dispatch API can be used in both Message and Payload modes.

For more information on the Dispatch API in JAX-WS 2.0 please refer to JAX-WS 2.0 Dispatch.

2.1.7 Asynchronous Client Operations

For more information on asynchronous clients in JAX-WS 2.0 please refer to JAX-WS 2.0 Asynchronous.

3. Programming Model

This section of the documentation will focus on the programming model for both developing and publishing a web service endpoint, and writing a web service client. A web service endpoint is the implementation of a web service. A web service client is an application that accesses a web service.

3.1 Server

When developing a web service endpoint, a developer may either start from a Java endpoint implementation class or from a WSDL file. A WSDL (Web Services Description Language) document describes the contract between the web service endpoint and the client. A WSDL document may include and/or import XML schema files used to describe the data types used by the web service. When starting from a Java class, the tools generate any portable artifacts as mandated by the spec. When starting from a WSDL file and schemas, the tools generate a service endpoint interface.

There is a trade-off when starting from a Java class or from a WSDL file. If you start from a Java class, you can make sure that the endpoint implementation class has the desirable Java data types, but the developer has less control of the generated XML schema. When starting from a WSDL file and schema, the developer has total control over what XML schema is used, but has less control over what the generated service endpoint and the classes it uses will contain.

3.1.1 Starting from Java

The basic process for deploying a web service from a Java class consists of two steps.

  1. Generate portable artifacts.
  2. Create a WAR file to deploy

3.1.1.1 Generate Portable Artifacts

Portable artifacts generated by JAX-WS 2.0 include zero or more JavaBean classes to aide in the marshaling of method invocations and responses, as well as service-specific exceptions.

In document/literal wrapped mode, two JavaBeans are generated for each operation in the web service. One bean is for invoking the other for the response. In all modes (rpc/literal and both document/literal modes), one JavaBean is generated for each service-specific exception.

When starting from Java the developer must provide the JAX-WS tools with a valid endpoint implementation class. This implementation class is the class that implements the desired web service. JAX-WS has a number of restrictions on endpoint implementation classes. A valid endpoint implementation class must meet the following requirements:

  • It must carry a javax.jws.WebService annotation (see JSR 181).
  • It may extend java.rmi.Remote either directly or indirectly.
  • Any of its methods may carry a javax.jws.WebMethod annotation (see 7.5.2).
  • All of its methods may throw java.rmi.RemoteException in addition to any service-specific exceptions.
  • All method parameters and return types must be compatible with the JAXB 2.0 Java to XML Schema mapping definition.
  • A method parameter or return value type must not implement the java.rmi.Remote interface either directly or indirectly.

Here is an example of a a simple endpoint implementation class AddNumbersImpl.java from the fromjava sample:

package fromjava.server;
import javax.jws.WebService;
@WebService
public class AddNumbersImpl {
/**
* @param number1
* @param number2
* @return The sum
* @throws AddNumbersException
* if any of the numbers to be added is negative.
*/
public int addNumbers(int number1, int number2) throws AddNumbersException {
if (number1 < 0 || number2 < 0) {
throw new AddNumbersException("Negative number cant be added!",
"Numbers: " + number1 + ", " + number2);
}
return number1 + number2;
}
}

If you are familiar with JAX-RPC 1.1, you will notice that this implementation class does not implement a service endpoint interface. In JAX-WS 2.0 a service endpoint interface is no longer required.

When starting from a Java endpoint implementation class, it is recommended that the portable artifacts be generated from source using apt. This because the JAX-WS tools will then have full access to the source code and will be able to utilize parameter names that are otherwise not available through the Java reflection APIs. If the source for the endpoint implementation class is not available, the portable artifacts can be generated using wscompile. Here is a sample apt Ant task from the samples:

<apt
debug="${debug}"
verbose="${verbose}"
destdir="${build.classes.home}"
sourcedestdir="${build.classes.home}"
sourcepath="${basedir}/src">
<classpath refid="jax-ws.classpath"/>
<option key="r" value="${build.home}"/>
<source dir="${basedir}/src">
<include name="**/server/*.java"/>
</source>
</apt>

More information about the apt Ant task can be found here. If this task is run on the fromjava sample, the output would include:

AddNumbers.class
AddNumbers.java
AddNumbersExceptionBean.class
AddNumbersExceptionBean.java
AddNumbersResponse.class
AddNumbersResponse.java

The AddNumbersImplService.wsdl file describes the web service. The schema1.xsd file is imported by the AddNumbersImplService.wsdl and contains the datatypes used by the web service. The AddNumbers.class/AddNumbers.java files contain the a bean used by a JAXB to marshall/unmarshall the addNumbers request. The AddNumbersExceptionBean.class/AddNumbersExceptionBean.java file is a bean used by JAXB to marshall the contents of the AddNumbersException class. The AddNumbersResponse.class/AddNumbersResponse.java files represent the response bean used by JAXB to marshall/unmarshall the addNumbers response.

3.1.1.2 Create a WAR file to deploy

Creating a WAR file is nothing more than packaging the service endpoint interface (if there is one), service endpoint implementation, Java classes used by the endpoint implementation and a deployment descriptor in WAR format.  For the fromjava sample the AddNumbersImpl and AddNumbersException classes in the fromjava.server package, and the deployment descriptor are bundled together to make a raw WAR file. To learn more about creating a WAR file and the deployment descriptor, click here.  The deployment descriptor used in fromjava sample is given below and can be found here:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name='fromjava'
implementation='fromjava.server.AddNumbersImpl'
url-pattern='/addnumbers'/>
</endpoints>

The attributes of the <endpoint> element are described below:

  • name is simply an identifier for this endpoint

  • implementation is used to specify the endpoint implementation class

  • urlpatter is used to URL pattern used to access this endpoint.

The structure of the raw WAR file is shown below:

META-INF/MANIFEST.MF
WEB-INF/sun-jaxws.xml
WEB-INF/web.xml
WEB-INF/classes/fromjava/server/AddNumbersException.class
WEB-INF/classes/fromjava/server/AddNumbersImpl.class
WEB-INF/classes/fromjava/server/jaxws/AddNumbers.class
WEB-INF/classes/fromjava/server/jaxws/AddNumbersExceptionBean.class
WEB-INF/classes/fromjava/server/jaxws/AddNumbersResponse.class

The WAR file created  can now be published on a JAX-WS 2.0 SI enabled servlet container such as the Sun Java System Application Server Platform Edition 8.1

3.1.2 Starting from a WSDL File

The basic process for deploying a web service when starting from a WSDL document consists of the following four steps:

  1. Generate a service endpoint interface.
  2. Implement the service endpoint interface.
  3. Create a WAR file to deploy.

3.1.2.1 Generate a Service Endpoint Interface

This step involves compiling or importing the WSDL file to generate a service endpoint interface and value classes mapped from imported XML schemas.

Below is a sample wsimport Ant target:

<wsimport
debug="${debug}"
verbose="${verbose}"
keep="${keep}"
destdir="${build.classes.home}"
wsdl="${server.wsdl}">
<binding dir="${basedir}/etc" includes="${server.binding}"/>
</wsimport>

Its commandline equivalent is:

wsimport.sh etc/AddNumbers.wsdl -b custom-server.xml

Lets look at the excerpt of AddNumbers.wsdl from the sample fromwsdl:

The generated service endpoint interface looks as follows:

package fromwsdl.server;

@javax.jws.WebService(
name="AddNumbersPortType",
serviceName="AddNumbersService",
targetNamespace="http://duke.org"
)
@javax.jws.soap.SOAPBinding(
style=javax.jws.soap.SOAPBinding.Style.DOCUMENT,
use=javax.jws.soap.SOAPBinding.Use.LITERAL,
parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
public interface AddNumbersPortType extends java.rmi.Remote {
@javax.jws.WebMethod(operationName="addNumbers")
@javax.jws.WebResult(name="return")
public int addNumbers(
@javax.jws.WebParam(name="arg0")
int arg0,
@javax.jws.WebParam(name="arg1")
int arg1) throws fromwsdl.server.AddNumbersFault_Exception, java.rmi.RemoteException;
}

The generated service endpoint interface has annotations that can be used by the future versions of JAX-WS 2.0 to do dynamic binding and serialization/deserialization at runtime. Alternatively this service endpoint interface can be used to generate a WSDL and schema file. Please note that round-tripping is not guaranteed in this case. So the generated WSDL file and schema may not be the same as the one the service endpoint interface was generated from.

3.1.2.2 Implement the Service Endpoint Interface

The next thing to do will be to provide the implementation of the service endpoint interface generated in the previous step. When you implement the service endpoint interface it is necessary to provide a @WebService annotation on the implementation class with a endpointInteface element specifying the qualified name of the endpoint interface class. Let's look at the implementation class fromwsdl.server.AddNumbersImpl.java from the sample application fromwsdl:

package fromwsdl.server;

@WebService(endpointInterface="fromwsdl.server.AddNumbersPortType")
public class AddNumbersImpl implements AddNumbersPortType {
/**
* @param number1
* @param number2
* @return The sum
* @throws AddNumbersException
* if any of the numbers to be added is negative.
*/

public int addNumbers(int number1, int number2)
throws AddNumbersFault_Exception {
...
}
}

3.1.2.3 Create a WAR

This step is similar to the one described above in 3.1.1.2 .

Here the service endpoint interface implementation class from previous step, together with a deployment descriptor file sun-jaxws.xml, and web.xml should be bundled together with the service endpoint interface, value classes generated in the first step mentioned in 3.1.2.1.

Let's look at sun-jaxws.xml from the sample application fromwsdl:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">

<endpoint
name="fromwsdl"
interface="fromwsdl.server.AddNumbersPortType"
implementation="fromwsdl.server.AddNumbersImpl"
wsdl="WEB-INF/wsdl/AddNumbers.wsdl"
service="{http://duke.org}AddNumbersService"
port="{http://duke.org}AddNumbersPort"
url-pattern="/addnumbers" />

</endpoints>

It defines the deployment-related configuration information for the fromwsdl endpoint.  You will notice that this deployment descriptor contains additional attributes than the deployment
descriptor described in 3.1.1.2.  The interface attribute references the service endpoint interface generated in step 1.  The wsdl attribute also points at the WSDL that was imported by wsimport.
The service attribute references which service in the WSDL this endpoint is from and the port is the name of the port in that service for this endpoint.

To learn more about creating a WAR file and the deployment descriptor, click here.

The WAR file created  can now be published on a JAX-WS 2.0 SI enabled servlet container such as the Sun Java System Application Server Platform Edition 8.1

3.1.2.4 J2SE Endpoints

Endpoints can be created and published programmatically using javax.xml.ws.Endpoint API in J2SE. To learn more about these endpoints, see here.

3.2 Client

A client application can access a remote web service endpoint in one of two ways: port and dispatch.

3.2.1 Dynamic Proxy

In this approach client side invokes Web services via a dynamic proxy.  The proxies for the Web Service are created from the generated Service and service endpont interfaces.  Once the proxies are created. the client application can invoke methods on those proxies just like a standard implmentation of those interfaces. The sections below describe this process more detail.

3.2.1.1 Generate Client Artifacts

The wsimport tool is used to generate the service endpoint interface and the service interface classes. Below is the sample wsimport Ant target:

 <wsimport
debug="${debug}"
verbose="${verbose}"
keep="${keep}"
destdir="${build.classes.home}"
wsdl="${client.wsdl}">
<classpath>
<path refid="jax-ws.classpath"/>
<pathelement location="${build.classes.home}"/>
</classpath>
<binding dir="${basedir}/etc" includes="${client.binding}"/>
</wsimport>

The command line equivalent of this Ant target is:

wsimport.sh -classpath client_classpath -d dest_dir -s src_dir -b custom-client.xml http://localhost:8080/jax-ws-fromwsdl/addnumbers?WSDL

For more details see the wsimport tool documentation.

Here is the excerpt from fromwsdl.client.AddNumbersClient.java in the fromjava sample application:

//get the port
AddNumbersPortType port = new AddNumbersService().getAddNumbersPort();
//invoke the remote method
int result = port.addNumbers(10, 20);

3.2.2 Dispatch

The Dispatch API is intended for advanced XML developers who prefer using XML constructs at the java.lang.transform.Source or javax.xml.soap.SOAPMessage level. For added convenience use of Dispatch with JAXB data binding object is supported. With the XML/HTTP Binding a javax.activation.DataSource can also be used. The Dispatch APIs can be used in both Message and Payloadmodes. The Dispatch API client with an XML/HTTP Binding can be used with REST Web Services. Please see the restful sample program for more infomation.

For more information on Dispatch in JAX-WS 2.0 please refer to JAX-WS 2.0 Dispatch.

4. Useful Information

Annotation Processing Tool (apt) – http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html.

Please use the JAXB 2.0 and JAX-WS 2.0 forum for feedback.

The JAX-WS project on Java.net is: http://jax-ws.dev.java.net.


Copyright © 2005 Sun Microsystems, Inc. All rights reserved.

Terms of Use; Privacy Policy; Copyright ©2013-2016 (revision 20160708.bf2ac18)
 
 
Close
loading
Please Confirm
Close