You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Developing a Service using JAX-WS

You can develop a service using one of two approaches:

  • Start with a WSDL contract and generate Java objects to implement the service.
  • Start with a Java object and service enable it using annotations.
    For new development the preferred path is to design your services in WSDL and then generate the code to implement them. This approach enforces the concept that a service is an abstract entity that is implementation neutral. It also means you can spend more time working out the exact interface your service requires before you start coding.

However, there are many cases where you may need to service enable an existing application. While JAX-WS eases the process, it does require that you make some changes to source code of your application. You will need to add annotations to the source. It also requires that you migrate your code to Java 5.0.

WSDL First Development

Using the WSDL first model of service development, you start with a WSDL document that defines the service you wish to implement. This WSDL document could be obtained from another developer, a system architect, a UDDI registry, or you could write it yourself. The document must contain at least a fully specified logical interface before you can begin generating code from it.

Once you have a WSDL document, the process for developing a JAX-WS service is three steps:

  1. Generate starting point code.
  2. Implement the service's operations.
  3. Publish the implemented service.

Generating the Starting Point Code

JAX-WS specifies a detailed mapping from a service defined in WSDL to the Java classes that will implement that service. The logical interface, defined by the wsdl:portType element, is mapped to a service endpoint interface (SEI). Any complex types defined in the WSDL are mapped into Java classes following the mapping defined by the Java Architecture for XML Binding (JAXB) specification. The endpoint defined by the wsdl:service element is also generated into a Java class that is used by consumers to access endpoints implementing the service.

The wsdl2java command automates the generation of this code. It also provides options for generating starting point code for your implementation and an ant based makefile to build the application. wsdl2java provides a number of arguments for controlling the generated code.

Running wsdl2java

You can generate the code needed to develop your service using the following command:
wsdl2java -ant -impl -server -d outputDir myService.wsdl

The command does the following:

  • The -ant argument generates a Ant makefile, called build.xml, for your application.
  • The -impl argument generates a shell implementation class for each portType element in the WSDL document.
  • The -server argument generates a simple main() to launch your service as a stand alone application.
  • The -d outputDir argument tells wsdl2java to write the generated code to a directory called outputDir.
  • myService.wsdl is the WSDL document from which code is generated.

Generated code

#Table1 describes the files generated for creating a service.

Table 1: Generated Classes for a Service

File

Description

portTypeName.java

The SEI. This file contains the interface your service implements. You should not edit this file.

serviceName.java

The endpoint. This file contains the Java class your clients will use to make requests on the service.

_portTypeName_Impl.java

The skeleton implementation class. You will modify this file to implement your service.

_portTypeName_Server.java

A basic server main() that allows you to deploy your service as a stand alone process.

Implementing the Service

Once the starting point code is generated, you must provide the business logic for each of the operations defined in the service's interface.

Generating the implementation code

You generate the implementation class for your service with wsdl2java's -impl flag.

Tip

If your service's contract included any custom types defined in XML Schema, you will also need to ensure that the classes for the types are also generated and available.

Generated code

The service implementation code consists of two files:

  • portTypeName.java is the service interface(SEI) for the service.
  • _portTypeName_Impl.java is the class you will use to implement the operations defined for the service.

Implement the operation's logic

You provide the business logic for your service's operations by completing the stub methods in _portTypeName_Impl.java. For the most part, you use standard Java to implement the business logic. If your service uses custom XML Schema types, you will need to use the generated classes for each type to manipulate them. There are also some CXF specific APIs that you can use to access some advanced features.

Example

For example, an implementation class for a service that defined the operations sayHi and greetMe may look like #Example1

Example 1: Implementation of the Greeter Service
package demo.hw.server;

import org.apache.hello_world_soap_http.Greeter;

@javax.jws.WebService(portName = "SoapPort", serviceName = "SOAPService",
 targetNamespace = "http://apache.org/hello_world_soap_http",
 endpointInterface = "org.apache.hello_world_soap_http.Greeter")

public class GreeterImpl implements Greeter {

 public String greetMe(String me)
{
        System.out.println("Executing operation greetMe");
        System.out.println("Message received: " + me + "\n");
        return "Hello " + me;
     }

 public String sayHi()
{
        System.out.println("Executing operation sayHi\n");
        return "Bonjour";
     }
}
  • No labels