English | Japanese

Exercise: Basic of Web Services 2

Invoking Web Services from Applications

This exercise is to practice how to invoke Web services in Java for application implementation. The detailed steps will also be illustrated using language services.

Required Environments

Steps for Invoking Web Services in Java

To invoke Web services in Java for application implementation, the following steps are required. We will also use machine translation service J-Server to describe the steps in details.

  • STEP1: Generate stub.
    • Download Web service framework Apache Axis 1 (Version 1.4 Final is recommended) and extract the package. You need some .jar files in lib folder (axis.jar, jaxrpc.jar, commons-logging-1.0.4.jar, commons-discovery-0.2.jar, saaj.jar, wsdl4j-1.5.1.jar).
    • Download WSDL of the machine translation service J-Server (KyotoUJServer.wsdl). If the download dialog is not shown, please right-click and save via the context menu. (Change the extension to .wsdl if the default extension is .xml.)
    • Generate stub source using WSDL2Java (provided as a part of Axis). The sample code (.bat script for Windows) is shown as follows.
    • Sample File: stub.bat
      set LIB=./lib
      set AXIS_CLASSPATH=%LIB%/axis.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/jaxrpc.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/commons-logging-1.0.4.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/commons-discovery-0.2.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/saaj.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/wsdl4j-1.5.1.jar
      
      set CLASSPATH=%AXIS_CLASSPATH%;.
      
      java -cp %CLASSPATH% org.apache.axis.wsdl.WSDL2Java KyotoUJServer.wsdl
      NOTE: The above script may output the warning message "Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.", but you can continue this exercise without any trouble.
    • We recommend that you create a new folder for this project and locate this script, the downloaded WSDL and the folder for libraries of Axis there (as shown in the following figure).
    • The environment variable LIB refers to the location of Axis libraries.
    • You may need to change names of jar files to use the above script (e.g. the versions of libraries might be changed).
    • Please refer to Axis User's Guide for detailed information of WSDL2Java.
  • STEP2: Create Java code for invoking language services.
    • The sample Java code for invoking the language service J-Server is shown as follows. This sample code should be placed in the same folder as the script generating stub.
    • Sample File: Test.java
      import java.net.URL;
      import org.apache.axis.client.Stub;
      import Translation.nlp.nict.servicetype.Kyoto1LangridKyotoUJServerLocator;
      import Translation.nlp.nict.servicetype.TranslationService;
      
      public class Test{
        public static void main(String[] args) throws Exception{
      
          // Translation
          Kyoto1LangridKyotoUJServerLocator jserverLoc = 
            new Kyoto1LangridKyotoUJServerLocator();
          TranslationService transService = jserverLoc.getTranslation(
            new URL("Input endpoint URL"));
          Stub stub = (Stub)transService;
          stub.setUsername("Your temporary ID or Language Grid ID");
          stub.setPassword("Your temporary password or Language Grid password");
          String ret = transService.translate("en", "ja", "Hello.");
          System.out.println(ret);
      
        }
      }
    • Set your own temporary ID, password and the endpoint URL. The endpoint URL is described as location attribute of <wsdlsoap:address> element in WSDL file.
    • You need to import some classes generated by WSDL2Java in the previous step. The classes implement service invocations as their methods.
    • We recommend that you should save your source code in UTF-8 because we handle various languages via our Web services.
  • STEP3: Test the created Java code.
    • Compile and run the Java code created in Step 2 to test the language service J-Server.
    • The following script compiles and executes the previous sample Java code (The lines followed by "java -Dhttp.proxyHost=..." should be described in one line in .bat script. Remove line feed characters between arguments if you copy this script). The result is output to "output.txt".
    • Sample File: run.bat
      set LIB=./lib
      set AXIS_CLASSPATH=%LIB%/axis.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/jaxrpc.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/commons-logging-1.0.4.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/commons-discovery-0.2.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/saaj.jar
      set AXIS_CLASSPATH=%AXIS_CLASSPATH%;%LIB%/wsdl4j-1.5.1.jar
      
      set CLASSPATH=%AXIS_CLASSPATH%;.
      
      javac -encoding UTF-8 -cp %CLASSPATH% Test.java
      java -Dhttp.proxyHost=YourProxy 
           -Dhttp.proxyPort=YourProxyPort 
           -Dhttp.nonproxyHost=localhost 
           -cp %CLASSPATH% Test > output.txt
      pause
      NOTE: The script also outputs the warning message "Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.", but correctly returns the translation result.
    • Note that you need a compile option for handling UTF-8.
    • Add Java VM options for proxy settings if you need. Set the proxy and the port for your environment by replacing red characters in the above script. Remove options -Dhttp.proxyHost=..., -Dhttp.proxyPort=..., -Dhttp.nonproxyHost=... if you don't need a proxy in your environment.

Try More Language Services

You can practice this exercise using the above steps for testing Web services for more language services provided by participants of the Language Grid. Try the following service as an advanced exercise.

  • Google Translate
    • Copyright: Google, Inc.
    • Supported languages: Refer to Service Profile
    • Operation: String translate (String sourceLang, String targetLang, String source)
      • sourceLang: Source language
      • targetLang: Target language
      • source: Sentence to be translated