Skip to main content

skip to main content

developerWorks  >  WebSphere | SOA and Web services | Rational  >

Message-level security with JAX-WS on WebSphere Application Server v7

Using Rational Application Developer 7.5.2 to build secure JAX-WS Web services

developerWorks
Go to the previous pagePage 3 of 12 Go to the next page

Document options
PDF format - Fits A4 and Letter

PDF - Fits A4 and Letter
2742 KB (58 pages)

Get Adobe® Reader®

Sample code


My developerWorks needs you!

Connect to your technical community


Rate this tutorial

Help us improve this content


Section 3. Creating and consuming JAX-WS Web services

In this section, you create a simple HelloWorld Web service using a JAX-WS annotation and the Rational Application Developer tooling. Then you use the Rational Application Developer tooling to generate a proxy client to invoke the Web service. Finally, you test your service provider (i.e. Web service) running on the WebSphere Test Environment (WTE) that comes packaged with Rational Application Developer. In your testing, you examine the SOAP messages (i.e. request and response) as they appear on the network.

Creating a JAX-WS Service Provider

In this tutorial, you will use a dynamic Web project to contain the Web service. Start by creating a dynamic Web project and a plain old Java object (POJO) for your service provider as shown in the following steps:

  1. Start Rational Application Developer 7.5.2. Click File > New > Dynamic Web Project, then enter HelloWorldProject as the project name, as shown in Figure 3.

    Figure 3. Create a dynamic Web project
    Screen shot of New Dynamic Web Project screen

  2. Accept the defaults for the other fields, then click the Finish button. Choose No if prompted to change to the Web perspective. For this tutorial, you will use the Java EE perspective.J2EE perspective icon

  3. Select the HelloWorldProject project in the project explorer view. Right-click and select New > Class, which brings up a Java Class wizard as shown in Figure 4.

    Figure 4. Create new Java class wizard
    Screen shot of Create Java Class wizard

  4. Enter com.ibm.dwexample for the package name and HelloWorldProvider as the class name , and click the Finish button.

  5. Copy the code from Listing 1 into the HelloWorldProvider.java file and save the file.

    Listing 1. HelloWorldProvider.java
    package com.ibm.dwexample;
    import javax.jws.WebService;
    
    @WebService
    public class HelloWorldProvider {
      public String sayHello(String msg) {
        System.out.println("[helloworld provider] Hello " + msg);
        return "Hello " + msg;
      }
    }
    

That’s it! A simple POJO with the @WebService annotation is all that is necessary to create a JAX-WS Web service.

Now that you have created a Web service, you can use Rational Application Developer to deploy your service onto the WebSphere Test Environment (WTE).

To deploy your service:

  1. Select the HelloWorldProvider.java file that contains the code from Listing 1. Right-click and choose Run As > Run on Server, which displays the Run On Server wizard in Figure 5.

    Figure 5. Run On Server wizard
    Screen shot of Run on Server wizard

  2. Select the Choose an existing server radio button, and then select WebSphere Application Server v7.0 at localhost. If you do not have a WebSphere Application Server v7 server defined, you can define a new one by selecting Manually define a new server followed by expanding the IBM folder and selecting the WebSphere Application Server v7.0 server.

  3. Click the Finish button to deploy and start the Web service on WebSphere Application Server.

At this point, you have created a simple JAX-WS Web service, deployed it to WTE, and started the service. Next we’ll create a JAX-WS client that can invoke the running Web service.



Back to top


Creating a JAX-WS service consumer

Rational Application Developer v7.5.2 provides a wizard for generating a client from a JAX-WS Web service. In this tutorial, you use a standalone Java client as your service consumer. Therefore, you begin by creating a simple Java project that will contain your JAX-WS client proxy as well as your client code that uses the generated client proxy.

To create a Java project:

  1. Using Rational Application Developer select File > New > Other. Locate the Java Project wizard and click Next.

  2. In the New Java Project wizard, enter HelloWorldConsumer as the project name and click Finish as shown in Figure 6. If prompted to switch to the Java perspective, choose No to stay in the Java EE perspective.

    Figure 6. New Java Project wizard
    Screen shot of New Java Project wizard

    Now that you have a Java project to hold the JAX-WS proxy classes that are generated by Rational Application Developer, you can use Rational Application Developer to generate the client proxy classes.

  3. Expand the Services folder of the HelloWorldProject Web project. Right-click the {http://dwexample.ibm.com/}HelloWorldProviderService service and choose Generate > Client as shown in Figure 7.

    Figure 7. Generate the Web service client
    Screen shot of menu selections for generating a Web service

  4. Ensure the Web service runtime value in the configuration section is set to IBM WebSphere JAX-WS, and Java Proxy is selected as the client type.

  5. Click the Client project: link to change the client project to use the standalone Java project you created above, as shown in Figure 8.

    Figure 8. Web Service Client wizard
    Screens how of Web Service Client wizard

  6. Choose HelloWorldConsumer as the client project for the generated Java proxy, and then click OK as shown in Figure 9.

    Figure 9. Specify the Client project for proxy
    Screen shot of Speficy Client Project Settings screen

  7. Click the Next button and enter com.ibm.dwexample.clientproxy as the target package of the Web service client. Ensure the Generate portable client checkbox is selected, as shown in Figure 10. It is usually a good idea to segregate the generated code from your client code by using a different package name for the proxy code as shown in this step.

    Figure 10. Web Service Client Configuration wizard
    Screen shot of Web Service Client wizard

  8. Click the Finish button to generate the client proxy code, which will look like Figure 11.

    Figure 11. Generated client proxy classes
    Screen shot of Project Explorer with the generated client proxy

Rational Application Developer uses the WSDL of the service provider to auto-generate Java classes that can invoke the service provider Web service. Figure 11 shows the classes that get generated for you. Using the generated proxy classes, you do not have to worry about SOAP message building, XML parsing, or any other low-level programming constructs – the generated classes do this for you. All you need to do is instantiate the client proxy and invoke methods you want to be sent to the Web service. Therefore, next you will create a simple Java test client that instantiates the generated client proxy in order to invoke the service provider.

To create a Java test client:

  1. Right-click the HelloWorldConsumer project and choose New > Class.

  2. Enter com.ibm.dwexample as the package for the client and ClientTest as the Java class name as shown in Figure 12. Note that you segregate your client code from the generated proxy client by using a different Java package name.

    Figure 12. New Java Class wizard
    Screen shot of New Java Class wizard

  3. Click the Finish button. Copy the code from Listing 2 into the ClientTest.java file and save the file.

    Listing 2. ClientTest.java
    1   package com.ibm.dwexample;
    2   import com.ibm.dwexample.clientproxy.HelloWorldProvider;
    3   import com.ibm.dwexample.clientproxy.HelloWorldProviderService;
    
    4   public class ClientTest {
    
    5     public static void main(String[] args) {
    6       try {
    7         HelloWorldProviderService srv = new HelloWorldProviderService();
    8         HelloWorldProvider port = srv.getHelloWorldProviderPort();
          
    9         String resp = port.sayHello("World");
    10        System.out.println("[response] " + resp);
    11      } catch(Exception e) {
    12        e.printStackTrace();
    13      }
    14    }
    15  }
    

In Listing 2, line 7 demonstrates how you instantiate the client proxy service that Rational Application Developer generated for you. Then in line 8, you use the generated interface (i.e. HelloWorldProvider) to get a handle to the Web service port. Finally you use the port object to invoke the sayHello() method which will make a remote call to the Web service provider. The invocation of port.sayHello(“World”)sends a SOAP request message to the listening Web service provider shown in Listing 1. The service provider then sends a SOAP response message back to the client. Let’s examine what these SOAP request and response messages look like via the built-in TCP/IP Monitor view provided by Rational Application Developer.



Back to top


Test and verify the consumer and provider

TCPMON is also available with the WebSphere installation. You can use the following command to invoke it:

java –cp com.ibm.ws.webservices.thinclient_7.0.0.jar com.ibm.ws.webservices.engine.utils.tcpmon

After you have the server-side and client-side up and running, it is generally a good idea to test and verify things. Rational Application Developer provides a TCP/IP Monitor view to display the SOAP message as it is transferred from the client to the server and back. To use this view, we need to configure the TCP/IP Monitor to listen on an unused TCP/IP port, and then update your client proxy code to point to this TCP/IP port. The following section demonstrates how to do this.

  1. In Rational Application Developer, select Window > Show View > Other. Then locate the TCP/IP Monitor view in the Debug folder and click OK.

  2. Right-click in the first entry box and choose Properties as shown in Figure 13.

    Figure 13. TCP/IP Monitor view
    Screen shot of TCP/IP Moniter view

  3. Click the Add button to configure a new monitor to intercept the Web service request and response in order to display the SOAP message.

  4. Enter an unused TCP port for the Local monitoring port field. (e.g. 9081)

  5. Enter localhost for the Host Name field.

  6. Enter the TCP port for the Web service provider (check the WSDL in the HelloWorldConsumer project). (e.g. 9080). You should have values similar to Figure 14:

    Figure 14. New Monitor settings
    Screen shot of New Monitor window

  7. Click the OK button.

  8. Now select your newly defined TCP/IP Monitor and click the Start button as shown in Figure 15:

    Figure 15. Start TCP/IP Monitor
    Screen shot of TCP/IP Monitor window

    Now that the TCP/IP Monitor is running and listening for Web service calls, you need to change your client proxy to connect to the listening port of the TCP/IP monitor instead of connecting directly to the service provider. You can accomplish this by changing the port number in the WSDL that was saved locally to the client project as a result of clicking the Generate portable client checkbox in Figure 10.

  9. Right-click the HelloWorldProviderService.wsdl file located in the HelloWorldConsumer project under the src > META-INF > wsdl folder path and choose Open With > WSDL Editor.

  10. Change the port number to match the TCP/IP Monitor listening port (e.g. 9081) as shown in Figure 16, then save and close the WSDL file.

    Figure 16. WSDL Editor (see enlarged Figure 16)
    Screen shot of WSDL Editor

  11. Right-click the ClientTest.java file and choose Run As > Java Application. The results should appear in the TCP/IP Monitor view as shown in Figure 17.

    Figure 17. ClientTest results (see enlarged Figure 71)
    Screen shot of TCP/IP Monitor view running the         ClientTest application

At this point, you have developed a JAX-WS Web service provider (i.e. server-side) and a JAX-WS Web service consumer (i.e. client-side) and demonstrated the results of the consumer invoking the provider. In the next section, you configure the policy sets to add message-level security to your little HelloWorld example, and once again view the results in the TCP/IP Monitor view to verify that the SOAP message is being passed securely.



Back to top



Go to the previous pagePage 3 of 12 Go to the next page