Developing JAX-WS Web Services for WebSphere

This tutorial outlines the process for developing a JAX-WS web service and deploying it to WebSphere using MyEclipse. The web service used in this tutorial is a calculator service that provides simple operations to the caller. Using JAX-WS with WebSphere requires a Blue or Bling subscription. In this tutorial, you will learn how to:

  • Create a Web Service project
  • Create a service class
  • Create a web service
  • Deploy and test the web service
  • Create a web service client

Duration Time: 30 Minutes

Start Tutorial Now

Don't have MyEclipse? Download Now


1. Create a Web Service Project

You can download the sample project created in this turorial. The sample is configured to run against WebSphere 6.1. You may need to adjust the target  server and/or the runtime JRE libraries used to build the projects to more closely match your particular build and deployment environment.

  1. Click the drop-down arrow , and select Web Service Project.

    Opening a new web service projectNote: A JAX-WS web service can be generated in any existing Java EE 5 Web project.
  2. Type WebServiceProject as the project name, select the JAX-WS framework option, and select your WebSphere version from the Target runtime drop-down.

    You might receive a warning if you haven't yet configured the WebSphere connector; you can do this later. Click Finish. 

    Note:
    This tutorial uses the default JavaEE 6 version; however, JavaEE 7 is an available option when creating new projects.

    New Web Service Project window

Now that you have a new web project, you can create the Java class to use as the basis of your web service.


2. Create a Service Class

The service class is nothing more than a plain Java class  that provides implementations for the methods you want to  expose as web service. In this tutorial, you will write a simple Calculator class that implements a few typical  calculator operations.

  1. Expand the WebServiceProject folder in the Explorer view, and right-click the src folder. Select New>Package.

    Creating a package for the class
  2. Type com.myeclipseide.ws in the Name field, and click Finish.

    New Java Package window
  3. Right-click the com.myeclipseide.ws package, and select New>Class.
  4. Type Calculator in the Name field, and click Finish. This creates a Calculator.java file in the package.

    New Java Class window

Calculator.java opens in the editor.

Calculator class file open in editor

This class is a calculator implementation that provides the following functions for two integers:

  • add
  • subtract
  • multiply
  • divide

The implementation of the class looks like:

public class Calculator { 
public int add(int a, int b) {
return (a + b);
}

public int subtract(int a, int b) {
return (a - b);
}

public int multiply(int a, int b) {
return (a * b);
}

public int divide(int a, int b) {
return (a / b);
}

This class is a very simple POJO offering four operations. There is no use of special annotations, interfaces, or base classes.

Copy the previous code, paste it into your class file between the brackets, and click .


3. Create a Web Service

Now that you have the service class written (Calculator.java), you need to create a web service that exposes that server class as a web service.

  1. Click .
  2. Select the JAX-WS (WebSphere) framework option, and the Create web service from Java class (Bottom-up scenario) option.

    New web service creation strategyThe bottom-up web service generation uses the target server to generate the correct stub and implementation classes for the web service you are hosting from that particular application server.

    If you have not set a target server or if you haven't yet configured WebSphere, click the Configure target server link.

    The project Properties window opens allowing you to set the targeted runtimes. You can open the project properties to change the targeted runtime at any time.

    Setting the target serverNote: If you have not configured the WebSphere connector, it will not be listed as a targeted runtime, and service generation will not be possible. Refer to WebSphere Connectors in MyEclipse.
  3. After the target runtime is set, click OK.
  4. Click Next to continue creating the web service. Select the Java bean that implements the operations for your web service. In this case, type com.myeclipseide.ws.Calculator in the Java class field, select the Generate WSDL in project checkbox, and click Finish.

    Setting the web service class

MyEclipse generates the web service stubs and binding classes necessary for this web service to be deployed to the target application server set in the first step.

Check your project contents to view the artifacts generated for you so the web service can be deployed to the target server.
Generated artifacts


4. Deploy the Project

  1. Right-click the project, and select Debug As (or Run As)>MyEclipse Server Application.

    Debugging as a server application
  2. Select the server to which you want to deploy your web project. In this case, select your WebSphere server, and click Finish.

    Selecting the server for deployment

MyEclipse performs the following steps automatically:

  • Packages the web project and deploys it as in Exploded mode (as opposed to Packaged mode) to the application server.
  • Starts the application server and loads the web project.

5. Connect to the Web Service to Test

  1. Load the Web Services explorer by clicking .
  2. Click on the Web Services Explorer toolbar, and click the WSDL Main link to open the Open WSDL page.

    Opening the WSDL page
  3. Type http://localhost:9080/WebServiceProject/CalculatorService?WSDL in the WSDL URL field, and click Go.

    The URL is broken down into the following components:

    http://localhost:9080 — The server is running on localhost, and the port it has been binding the web projects to is the default port of 9080. To confirm this, you can watch the Console view while the server deploys a new web project. It displays the ports it binds the web project to as it deploys it.

    /WebServiceProject — By default, the Web Context-root used to deploy a web project matches the name of the project. Because we didn't customize the Web Context-root for this project, it is the same name as the project.

    /CalculatorService — When the JAX-WS web service was generated, it was bound to the CalculatorService WSDL.

    ?WSDL — This is a universal query string argument that, when added to the end of a web service, tells the web service to return its full WSDL to the caller. In this case, the WSDL is returned to the Web Services Explorer tool, which loads it and displays the web services exposed operations.<br/ >
    The Web Services Explorer loads all the operations exposed to us from this web service.
    Web service exposed operations
  4. Click the Add operation in the Navigator to use the explorer to test it. The Invoke a WSDL Operation page appears in the Explorer. The page displays the endpoint you are testing (Calculator), and each argument the operation takes along with a field for entering test values.
  5. Type the values 10 and 20 in the a and b fields, respectively, and click Go.

    Testing the add operation

The Status displays the response from the web service, which in this case is 30.


6. Create a Client for the Web Service

The web service client allows you to interact directly with the web service and its exposed operations without writing all the marshaling or connection code.

In MyEclipse, while you are generating a web service, you are given the option to generate a client for the web service at the same time. You can use this feature any time, but for this tutorial we felt a more typical situation is needing to create a web service client against a service that has already been deployed rather than one you are generating.

In this section, generate a web service client in a separate Java project, because web service is already deployed and you want to hook up to it. This keeps the lines between web service and web service client creation very clearly separated for this tutorial.

  1. Click the drop-down arrow , and select Java Project.
  2. Type WebServiceClientProject in the Project name field.
  3. Select the Use a Project Specific JRE options, and select your WebSphere application server from the drop-down list. This includes the JRE used to build and run this Java project that matches the runtime used for the application server to which you are deploying.

    New Java Project window
  4. Click the drop-down arrow , and select New Web Service Client.
  5. Select your Web Service Client project from the Project drop-down, select the JAX-WS (WebSphere) framework option, and select your WebSphere server as the Target Server. This provides the web service generation services that MyEclipse invokes to create your JAX-RPC client. Click Next.

    Creating a new web service client
  6. Be sure the WSDL URL option is selected, and type http://localhost:9080/WebServiceProject/CalculatorService/WEB-INF/wsdl/CalculatorService.wsdl in the WSDL URL field. The URL is extended to locate the CalculatorService.wsdl file.

    Specifying the WSDL URL for the client
  7. Click Next. MyEclipse loads the WSDL for the web service validates it for you, letting you know of any problems that might exist with the WSDL.

    Note:
    If any error occurs with validation, be sure the web service is deployed and the application server hosting it is running. If you are trying to generate a client to a 3rd party web service and you get errors during the validation process, bring it to the attention of the author of the web service, if possible, so it can be corrected.


    Validation status
  8. Click Finish to have MyEclipse generate the client for you.

    After the client has been generated, a new package appears in your src folder as well as a few new classes you can use to work with your web service.

    Generated classes in the SRC folder

    With the new generated resources you can use the CalculatorDelegate class to access a reference to the web service and then execute the operations you exposed (add, subtract, multiply and divide).

    As an example let's say we want to compute the following four things:

    • Add 3 and 7
    • Subtract 2 from 12
    • Multiply 9 by 9
    • Divide 40 by 2

    6.1 Using the Web Service Operations

    In this example, add code that uses the operations from the web service to make these calculation. First, you need to create a new class with a main method in it.

    1. Right-click the com.myeclipseide.ws package in the src folder of WebServiceClientProject, and select New>Class.
    2. Name it WebServiceClient, select the Public static void main checkbox, and click Finish.

      New Java class to contain the main method

      After you create the class, you need to provide an implementation of the main method so it does the four mathematical calculations listed listed above. The code to do perform those calculations with the web service and then print the results to the console is as follows:

      public static void main(String[] args) { 
      /* Create the service instance */
      CalculatorService service = new CalculatorService();
      CalculatorDelegate delegate = service.getCalculatorPort();

      /* Using the web service, perform the 4 calculations */
      System.out.println("1. 3+7=" + delegate.add(3, 7));
      System.out.println("2. 12-2=" + delegate.subtract(12, 2));
      System.out.println("3. 9*9=" + delegate.multiply(9, 9));
      System.out.println("4. 40/2=" + delegate.divide(40, 2));
      }
    3. Replace the public static void main function with the code shown above, and click .

      Code for the main method in the new Java class
    4. Run the WebServiceClient class by right-clicking it, and selecting Run As (or Debug As)>Java Application.

      Running the class as a Java application

    The client code runs, accesses the web service using the locator class generated for it, and then the following output appears in the console:


    Console output from the web service client