Web Services: Hello World Application

Sang Shin, sang.shin@sun.com, www.javapassion.com/webservices



In this lab, you are going to build a simplest possible sample Web services and client.


Expected duration: 60 minutes


Software Needed


Change Log


Things to be done (by Sang Shin)


Lab Exercises



Exercise 1: Build "HelloWorld" Web Service and Client using POJO class with Annotations

In this exercise, you are going to build a simple "HelloWorld" Web service by adding @WebService annotation to the Hello POJO class.
  1. Build, deploy, and test "HelloWorld" Web service
  2. Build and run "HelloWorld" Web service client

(1.1) Build, deploy, and test "HelloWorld" Web service


In this step, you are going to build, deploy, and test "HelloWorld" Web service.  We will name the project as HelloWebService.

0. Start NetBeans IDE if you have not done so yet.
1. Create a new NetBeans project.




Figure-1.11: New Web Application


2. Create a POJO class called Hello.java.  This POJO class will be exposed as a Web service in subsequent steps.

Figure-1.12: Create Hello.java POJO class

Figure-1.13: Name the class as Hello.java under mypackage
3. Add a business method to the Hello.java.
package mypackage;

/**
 *
 * @author sang
 */
public class Hello {

    // Business method we want to expose as
    // Web service operation
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}
Figure-1.14: Add sayHello() method

4.  Expose all public methods of the Hello.java as Web service operations.
package mypackage;

/**
 *
 * @author sang
 */
@WebService()
public class Hello {

    // Business method we want to expose as
    // Web service operation
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}
Code-1.15: Add @WebService annotation.





<NetBeans tip>

Figure-1.16: Code-completion of annotation

5. Deploy the application.  Once deployed, the Web service can be accessed by a Web servlce client.  As part of the deployment process, WSDL document of the Web service  is also created and deployed. 

Figure-1.17: Deploy the application

6. Test the Web service.

Figure-1.18: Test Web Service

Figure-1.19: HelloService Web Service Tester

Figure-1.20: Result of the HelloService Web service testing

7.  Access the WSDL document

Figure-1.21: Access WSDL document

Figure-1.22: WSDL document

                                                                                                                        return to top of the exercise


(1.2) Build and run Web Service client


In this step, you are going to build HelloWorld Web service client in the form of standalone Java application which accesses the Web service that is deployed in the above step.

1. Create a new NetBeans project.




2. Create a Web Service client.

Figure-1.23: Create Web Service Client


Figure-1.24: WSDL and Client Location

Figure-1.25: Click Yes for the question

Note: You can also select Project option (instead of WSDL URL option) as shown below in Figure-1.26, Figure-1.27, Figure-1.28.


Figure-1.26: Create a Web service client using Project


Figure-1.27: Select a Web service


Figure-1.28: Give a package name

3. Write Web service client code leveraging NetBeans code generation wizard.

Figure-1.29: Call Web Service Operation

Figure-1.30: Select Operation to Invoke
package hellowebserviceclient;

/**
 *
 * @author sang
 */
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try { // Call Web Service Operation
            mypackage.HelloService service = new mypackage.HelloService();
            mypackage.Hello port = service.getHelloPort();
            // TODO initialize WS operation arguments here
            java.lang.String arg0 = "Sang Shin";
            // TODO process result here
            java.lang.String result = port.sayHello(arg0);
            System.out.println("Result = "+result);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }      
       
    }
   
}
Code-1.31: Provide argument value

4. Add JAX-WS 2.1 library.









5. Run the Web service client application

Figure-1.32: Result of running Web Service client application

                                                                                                                        return to top of the exercise


Trouble-shooting: If you experience the following problem as shown in Figure-1.33 below, it is likely due to the fact that you did "Clean" for the project. 
Solution: Right click the project and select Clean and Build and then select Run.





Solution


The solutions to this exercise are provided as a ready-to-open-and-run NetBeans projects as part of hands-on lab zip file.  You have to run HelloWebService first, then HelloWebServiceClient.

Summary

In this exercise,  you have built and deployed a simple HelloWorld Web service.  You have learned building a Web service is a matter of adding @WebService() annotation to a POJO class.  You also have built and run the Web service client to access the Web service. 

                                                                                                                        return to the top


Exercise 2: Use WS Monitor to capture the SOAP traffic


WS Monitor is a Java tool, which you can use to capture the SOAP traffic.  Because it iplays the role of "man in the middle", the client has to send SOAP requests to port 4040 (which is configured as a default listening port of the WS Monitor) instead of port 8080.

1.  Start the WS Monitor.


2. Modify the Main.java to send SOAP requests to port 4040.  The code fragments that need to be added are highlighted in bold font.

package hellowebserviceclient;

import javax.xml.ws.BindingProvider;

/**
 *
 * @author sang
 */
public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try { // Call Web Service Operation
            mypackage.HelloService service = new mypackage.HelloService();
            mypackage.Hello port = service.getHelloPort();
           
            // Change the destination port to 4040
            BindingProvider bp = (BindingProvider) port;
            String address = (String)bp.getRequestContext().get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
            address = address.replaceFirst("8080", "4040");
            bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
            //
           
            // TODO initialize WS operation arguments here
            java.lang.String arg0 = "Sang Shin";
            // TODO process result here
            java.lang.String result = port.sayHello(arg0);
            System.out.println("Result = "+result);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }
        // TODO code application logic here
       
    } 
   
}

3. Observe that the WS Monitor now captures the SOAP traffic.



  Note:  As a default, WS Monitor is configured to use port 8080 as a final destination port number.  If you want to change this default destination port number from 8080 to some another port number (for example,  port 8089) , take the following steps:

Solution


You can find HelloWebServiceClientWSMonitor project as <LAB_UNZIPPED_DIRECTORY>/wshelloworld/solutions/HelloWebServiceClientWSMonitor.



Exercise 3: Perform context-sensitive Javadoc


In this exercise, you are going to configure javadoc file of the JAX-WS 2.1 library first and then perform context sensitive Javadoc'ing on JAX-WS annotation classes.

(3.1)  Configure Javadoc zip file of JAX-WS library


1. Open Library Manager.

Figure-3.10: Open Library Manager

2. Configure jaxws-2_1-api.doc.zip.
Figure-3.11: Add ZIP/Folder

Figure-3.12: Select jaxws-2_1-api.doc.zip
Figure-3.13: Benign error dialog box

3. Restart NetBeans.

Figure-3.14: Exit NetBeans
                                                                                                                        return to top of the exercise

(3.2)  Perform context sensitive Javadoc


1. Display Javadoc on @WebService() annotation.

Figure-3.21: Show Javadoc on @WebService() annotation

Figure-3.22: Javadoc of the @WebService() annotation

Trouble-shooting: If you still encounter the following condition, restart NetBeans.  If you still encounter the same condition even after the restart of NetBeans, configure Javadoc zip file for the JDK 6 platform as well.












                                                                                                                        return to top of the exercise



Homework Exercise (for people who are taking Sang Shin's "Web Services & SOA Programming online course")



1. The homework is to modify the HelloWebService project  you built in Exercise 1 above as following. (You might want to create a new project by copying the HelloWebService project if you want to preserve the original project.  You can name the homework project in any way you want but here I am going to call it myHelloWebService.)
public int add (int x, int y){
      return x+y;
}

2. Send the following files to  webserviceshomework@sun.com with Subject as WebServices-wsHelloWebService.

                                                                                                                    return to the top