Web Services in Java

What are you, a shut-in? Web services are how the world works now! Evolve already, you trogs!

1. If you have not already done so, download and install the Java SDK. Details are given in a previous tutorial. Make a note of the directory in which “javac.exe” is installed.

2. In any convenient location, create a new directory named “WebServiceTest”.

3. In the newly created WebServiceTest directory, create a new text file named “JavaPathSet.bat”, containing the following text. Substitute the name of the directory containing “javac.exe” in the appropriate place.

set javaPath="[name of the directory containing javac.exe]"

4. Still in the newly created WebServiceTest directory, create a new directory named “Service” and then navigate to it.

5. In the newly created Service directory, create a new text file named “ServiceBuildAndRun.bat”, containing the following text.

call ..\JavaPathSet.bat

mkdir WebServiceTestPackage

%javaPath%\javac.exe -g *.java

move *.class WebServiceTestPackage

%javaPath%\wsgen.exe -wsdl -classpath . WebServiceTestPackage.WebServiceTest

%javaPath%\java.exe -classpath . WebServiceTestPackage.WebServiceTest

pause

6. Still in the Service directory, create a new text file named “WebServiceTest.java” containing the following text. (This example assumes that you don’t already have anything already running at port 8080 on localhost.)

package WebServiceTestPackage;

import javax.jws.*;

@WebService()
public class WebServiceTest
{
    public void WebServiceTest() {}

    public static void main(String[] args) 
    {
        String serviceURL = "http://localhost:8080/WebServiceTestService";

        javax.xml.ws.Endpoint.publish
        (
            serviceURL,
            new WebServiceTest()
        );

        System.out.println("Service is running at " + serviceURL + "...");
    }

    // web methods

    @WebMethod()
    public String echo(String messageToEcho) 
    {
        return messageToEcho;
      }

    @WebMethod()
    public NumberPair swapNumbersInPair(NumberPair pairToSwap) 
    {
        pairToSwap.swapValues();
        return pairToSwap;
      }
}

7. Still in the Service directory, create a new text file named “NumberPair.java”, containing the following text.

package WebServiceTestPackage;

public class NumberPair implements java.io.Serializable
{    
    public int first;
    public int second;

    public NumberPair() 
    {
        // a default constructor is required
        // for web service marshalling
    }

    public NumberPair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }

    public void swapValues()
    {
        int temp = this.first;
        this.first = this.second;
        this.second = temp;
    }
}

8. Double-click the icon for ServiceBuildAndRun.bat to run it. A console window will appear, the web service will be built, and a message will be displayed saying that the service is running. Do not close the console window: it should stay open for the rest of the tutorial.

9. From the Service directory, navigate up one level to the WebServiceTest directory.

10. In the WebServiceTest directory, create a new directory named “Client” and then navigate to it.

11. In the newly created Client directory, create a new text file named “ClientBuildAndRun.bat” containing the following text.

call ..\JavaPathAndProgramNameSet.bat

%javaPath%\wsimport.exe http://localhost:8080/WebServiceTestService?wsdl 

%javaPath%\javac.exe -g -classpath . WebServiceTestClient.java

%javaPath%\java.exe -classpath . WebServiceTestClient

pause

12. Still in the Client directory, create a new text file named “WebServiceTestClient.java” containing the following text.

import javax.xml.ws.WebServiceRef;
import webservicetestpackage.*;

public class WebServiceTestClient
{
    @WebServiceRef(wsdlLocation="http://localhost:8080/WebServiceTestService/WebServiceTest?wsdl")

    private static WebServiceTestService service;

    public static void main(String[] args)
    {
        WebServiceTestClient client = new WebServiceTestClient();
        client.testProcedure();
    }

    public void testProcedure()
    {
        this.service = new WebServiceTestService();
        WebServiceTest webServiceTest = this.service.getWebServiceTestPort();

        System.out.println("About to attempt to call service method echo()...");

        String stringToEcho = "Echo!";
        System.out.println("   String to echo is: " + stringToEcho);

        String responseFromService = webServiceTest.echo(stringToEcho);
        System.out.println("   Response from service is: " + responseFromService);

        System.out.println("About to attempt to call service method swapNumbersInPair()...");

        NumberPair pairToSwap = new NumberPair();
        pairToSwap.setFirst(1);
        pairToSwap.setSecond(2);
        System.out.print("   Number pair to swap is ");
        this.printNumberPair(pairToSwap);

        NumberPair pairSwapped = webServiceTest.swapNumbersInPair(pairToSwap);

        System.out.print("   Number pair after swap is ");
        this.printNumberPair(pairSwapped);
    }

    private void printNumberPair(NumberPair pairToPrint)
    {
        System.out.println
        (
            "(" 
            + pairToPrint.getFirst() 
            + ", " 
            + pairToPrint.getSecond()
            + ")"
        );
    }
}

13. Double-click the icon of the ClientBuildAndRun.bat file to run it. A console window will appear, and the web service client will be built and executed. The results of the test procedure will appear in the console window.

Notes

  • The parameters and return types of the web service methods in this tutorial include not only primitive types, as in the strings passed to and returned by the echo() method, but also complex reference types, like the NumberPair instances passed into the swapNumbersInPair(). Using complex reference types with web services in this way is a very complicated matter. In this tutorial, the issue is mostly ignored. The wsimport command is being called in the ClientBuildAndRun.bat batch file, which generates proxy objects with the same names as the complex types used in the web service, but these aren’t really the same objects. None of the methods on the original object can be called on the client proxy. Also, the member variables, which are public on the server side, must be accessed from automatically generated accessor methods on the client-side proxy objects.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s