This tutorial will show you how to create client and endpoint communication with WS-Addressing enabled. Creating a WS-Addressing based service and client is very simple. The first step is to create regular JAX-WS service and client configuration; the last step is to configure the addressing on both sides.
The Service
We will start with the following endpoint implementation.
package org.jboss.test.ws.jaxws.samples.wsa;
import javax.jws.web service;
@web service
(
portName = "AddressingServicePort",
serviceName = "AddressingService",
targetNamespace = "http://www.jboss.org/jbossws/ws-extensions/wsaddressing",
endpointInterface = "org.jboss.test.ws.jaxws.samples.wsa.ServiceIface"
)
public class ServiceImpl implements ServiceIface
{
public String sayHello()
{
return "Hello World!";
}
}
package org.jboss.test.ws.jaxws.samples.wsa;
import javax.jws.web service;
@web service
(
portName = "AddressingServicePort",
serviceName = "AddressingService",
targetNamespace = "http://www.jboss.org/jbossws/ws-extensions/wsaddressing",
endpointInterface = "org.jboss.test.ws.jaxws.samples.wsa.ServiceIface"
)
public class ServiceImpl implements ServiceIface
{
public String sayHello()
{
return "Hello World!";
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The above endpoint implements the following endpoint interface:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Let us say that compiled endpoint and interface classes are located in directory /home/username/wsa/cxf/classes. The next step is to generate the JAX-WS artifacts and WSDL that will be part of the endpoint archive.
Generating WSDL and JAX-WS Endpoint Artifacts
We will use the wsprovide command line tool to generate WSDL and JAX-WS artifacts. Here's the command:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The above command generates the following artifacts:
Compiled classes
SayHello.class
SayHelloResponse.class
Java Sources
SayHello.java
SayHelloResponse.java
Contract Artifacts
AddressingService.wsdl
All previously mentioned generated artifacts will be part of the endpoint archive, but before we create the endpoint archive, we need to reference generated WSDL from the endpoint. We will use the wsdlLocation annotation attribute. This is the updated endpoint implementation before it is packaged to the war file:
package org.jboss.test.ws.jaxws.samples.wsa;
import javax.jws.web service;
@web service
(
portName = "AddressingServicePort",
serviceName = "AddressingService",
wsdlLocation = "WEB-INF/wsdl/AddressingService.wsdl",
targetNamespace = "http://www.jboss.org/jbossws/ws-extensions/wsaddressing",
endpointInterface = "org.jboss.test.ws.jaxws.samples.wsa.ServiceIface"
)
public class ServiceImpl implements ServiceIface
{
public String sayHello()
{
return "Hello World!";
}
}
package org.jboss.test.ws.jaxws.samples.wsa;
import javax.jws.web service;
@web service
(
portName = "AddressingServicePort",
serviceName = "AddressingService",
wsdlLocation = "WEB-INF/wsdl/AddressingService.wsdl",
targetNamespace = "http://www.jboss.org/jbossws/ws-extensions/wsaddressing",
endpointInterface = "org.jboss.test.ws.jaxws.samples.wsa.ServiceIface"
)
public class ServiceImpl implements ServiceIface
{
public String sayHello()
{
return "Hello World!";
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The created endpoint war archive consists of the following entries:
jar -tvf jaxws-samples-wsa.war
0 Mon Apr 21 20:39:30 CEST 2008 META-INF/
106 Mon Apr 21 20:39:28 CEST 2008 META-INF/MANIFEST.MF
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/
593 Mon Apr 21 20:39:28 CEST 2008 WEB-INF/web.xml
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/classes/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/
374 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/ServiceIface.class
954 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/ServiceImpl.class
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/
703 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/SayHello.class
1074 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/SayHelloResponse.class
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/wsdl/
2378 Mon Apr 21 20:39:28 CEST 2008 WEB-INF/wsdl/AddressingService.wsdl
jar -tvf jaxws-samples-wsa.war
0 Mon Apr 21 20:39:30 CEST 2008 META-INF/
106 Mon Apr 21 20:39:28 CEST 2008 META-INF/MANIFEST.MF
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/
593 Mon Apr 21 20:39:28 CEST 2008 WEB-INF/web.xml
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/classes/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/
374 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/ServiceIface.class
954 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/ServiceImpl.class
0 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/
703 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/SayHello.class
1074 Mon Apr 21 20:39:26 CEST 2008 WEB-INF/classes/org/jboss/test/ws/jaxws/samples/wsa/jaxws/SayHelloResponse.class
0 Mon Apr 21 20:39:30 CEST 2008 WEB-INF/wsdl/
2378 Mon Apr 21 20:39:28 CEST 2008 WEB-INF/wsdl/AddressingService.wsdl
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Copy to ClipboardCopied!Toggle word wrapToggle overflow
We have added the JAX-WS 2.1 Addressing annotation to configure WS-Addressing. The next step is to repackage the endpoint archive to apply this change.
Updating Client Code to Configure WS-Addressing
We need to update client implementation to configure WS-Addressing. Here's the updated client code:
package org.jboss.test.ws.jaxws.samples.wsa;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.AddressingFeature;
public final class AddressingTestCase
{
private final String serviceURL = "http://localhost:8080/jaxws-samples-wsa/AddressingService";
public static void main(String[] args) throws Exception
{
// construct proxy
QName serviceName = new QName("http://www.jboss.org/jbossws/ws-extensions/wsaddressing", "AddressingService");
URL wsdlURL = new URL(serviceURL + "?wsdl");
Service service = Service.create(wsdlURL, serviceName);
ServiceIface proxy = (ServiceIface)service.getPort(ServiceIface.class, new AddressingFeature());
// invoke method
proxy.sayHello();
}
}
package org.jboss.test.ws.jaxws.samples.wsa;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.AddressingFeature;
public final class AddressingTestCase
{
private final String serviceURL = "http://localhost:8080/jaxws-samples-wsa/AddressingService";
public static void main(String[] args) throws Exception
{
// construct proxy
QName serviceName = new QName("http://www.jboss.org/jbossws/ws-extensions/wsaddressing", "AddressingService");
URL wsdlURL = new URL(serviceURL + "?wsdl");
Service service = Service.create(wsdlURL, serviceName);
ServiceIface proxy = (ServiceIface)service.getPort(ServiceIface.class, new AddressingFeature());
// invoke method
proxy.sayHello();
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
We now have both JAX-WS client and endpoint communicating with each other using WS-Addressing.