Describe creating a Web Service using JAX-WS.


The starting point for developing a JAX-WS web service is a Java class annotated with the javax.jws.WebService annotation. The @WebService annotation defines the class as a web service endpoint.

A service endpoint interface or service endpoint implementation (SEI) is a Java interface or class, respectively, that declares the methods that a client can invoke on the service. An interface is not required when building a JAX-WS endpoint. The web service implementation class implicitly defines an SEI.

You may specify an explicit interface by adding the endpointInterface element to the @WebService annotation in the implementation class. You must then provide an interface that defines the public methods made available in the endpoint implementation class.

You use the endpoint implementation class and the wsgen tool to generate the web service artifacts that connect a web service client to the JAX-WS runtime.

Together, the wsgen tool and the Application Server provide the Application Server's implementation of JAX-WS.

These are the basic steps for creating the web service and client:

  1. Code the implementation class.

  2. Compile the implementation class.

  3. Use wsgen to generate the artifacts required to deploy the service.

  4. Package the files into a WAR file.

  5. Deploy the WAR file. The web service artifacts (which are used to communicate with clients) are generated by the Application Server during deployment.

  6. Code the client class.

  7. Use wsimport to generate and compile the web service artifacts needed to connect to the service.

  8. Compile the client class.

  9. Run the client.

Requirements of a JAX-WS Endpoint

JAX-WS endpoints must follow these requirements:

Coding the Service Endpoint Implementation Class

In this example, the implementation class, Hello, is annotated as a web service endpoint using the @WebService annotation. Hello declares a single method named sayHello, annotated with the @WebMethod annotation. @WebMethod exposes the annotated method to web service clients. sayHello returns a greeting to the client, using the name passed to sayHello to compose the greeting. The implementation class also must define a default, public, no-argument constructor.

package helloservice.endpoint;

import javax.jws.WebService;

public class Hello {

    private String message = new String("Hello, ");

    public void Hello() {}

    public String sayHello(String name) {
        return message + name + ".";

A Simple JAX-WS Client

HelloClient is a stand-alone Java program that accesses the sayHello method of HelloService. It makes this call through a port, a local object that acts as a proxy for the remote service. The port is created at development time by the wsimport tool, which generates JAX-WS portable artifacts based on a WSDL file.

When invoking the remote methods on the port, the client performs these steps:

  1. Uses the annotation to declare a reference to a web service. @WebServiceRef uses the wsdlLocation element to specify the URI of the deployed service's WSDL file:

    static HelloService service;

  2. Retrieves a proxy to the service, also known as a port, by invoking getHelloPort on the service.

    Hello port = service.getHelloPort();
    The port implements the SEI defined by the service.

  3. Invokes the port's sayHello method, passing to the service a name.

    String response = port.sayHello(name);								

Here is the full source of HelloClient:

package simpleclient;

import helloservice.endpoint.HelloService;
import helloservice.endpoint.Hello;

public class HelloClient {

    static HelloService service;

    public static void main(String[] args) {
        try {
            HelloClient client = new HelloClient();
        } catch(Exception e) {

    public void doTest(String[] args) {
        try {
            System.out.println("Retrieving the port from
                     the following service: " + service);
            Hello port = service.getHelloPort();
            System.out.println("Invoking the sayHello operation
                     on the port.");

            String name;
            if (args.length > 0) {
                name = args[0];
            } else {
                name = "No Name";

            String response = port.sayHello(name);
        } catch(Exception e) {

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz