Last modified: 11/7/2005
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 SI 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.
import javax.xml.ws.ServiceFactory;JAXWS 2.0 EA2 fromwsdl.client.AddNumbersClient.java creates proxy port (there is no generated stub):
ServiceFactory serviceFactory = ServiceFactory.newInstance();
AddNumbersService service = (AddNumbersService)serviceFactory.loadService(AddNumbersService.class);
AddNumbersPortType port = service.getAddNumbersPort();
import javax.xml.ws.ServiceFactory;
ServiceFactory serviceFactory = ServiceFactory.newInstance();
AddNumbersService service = (AddNumbersService)serviceFactory.createService((java.net.URL)null, AddNumbersService.class);
AddNumbersPortType port = service.getAddNumbersPort();
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.
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.
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.
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:
The following WSDL component's mapped Java names can be modified:
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.
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.
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.
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.
For more information on asynchronous clients in JAX-WS 2.0 please refer to JAX-WS 2.0 Asynchronous.
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.
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.
The basic process for deploying a web service from a Java class consists of two steps.
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:
javax.jws.WebService
annotation (see JSR 181).java.rmi.Remote
either
directly or indirectly.javax.jws.WebMethod
annotation (see 7.5.2).java.rmi.RemoteException
in addition to any service-specific exceptions.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.
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
The basic process for deploying a web service when starting from a WSDL document consists of the following four steps:
.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.
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 {
...
}
}
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
A client application can access a remote web service endpoint in one of two ways: port and dispatch.
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.
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:
//create the service factory
ServiceFactory serviceFactory = ServiceFactory.newInstance();
//load the service implementation class
AddNumbersService service = (AddNumbersService)serviceFactory.createService((java.net.URL)null, AddNumbersService.class);
//get instance of generated stub
AddNumbersPortType port = service.getAddNumbersPort();
//invoke the remote method
int result = port.addNumbers(10, 20);
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 Payload
modes.
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.
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.