Paul Labis: This is a personal blog where everyone can read my experiences, ideas and thoughts about programming, latest gadgets and other information technology related topics, lifestyle and many other stuff I would like to share to the public.

Web Service Tutorial in Java

Web Service is a piece of software that makes a service available on web. It uses a universal standard XML processing. It is interoperable in a sense that it enables a .NET software on a Windows Server to communicate to a JAVA software on a Unix. In other words, its not bound to a specific programming language or an operating system.
  • "Provider agent" implements a Web service
  • "Requester agent" (a.k.a. "client") makes use of that Web service
  • WSD(Web Service Description) represents a "contract"
  • Specifies the message formats, transport protocols, locations 
What we want to do in this article is to create a project where it utilizes a web service online. I plot the step by step way of utilizing a web service through terminal.


Please note that before you start the tutorial, you should make sure that you already setup Java on your ubuntu or linux operating system. Throughout this tutorial, we are not going to use eclipse or any IDE to acomplish our objective of utilizing a remote web service.

1. Find a Remote Web Service/ Webservice

There are a lot of sample remote web services to play on in www.webservicex.net. For the purpose of this tutorial. I would like to utilize Translator Engine Web Service (http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=47). We are going to use wsimport to generates portable JAX-WS artifacts for invoking a web service.

2. Create a dynamic proxy from WSDL(Web Service Description Language)

We will make it structural by creating a directory : ./TranslatorWS
Run generator on terminal by either $JAVA_HOME/Commands or $JAVA_HOME/bin/wsimport URL
In our case that is:
$ /usr/lib/jvm/java-6-sun-1.6.0.16/bin/wsimport -d ./TranslatorWS http://www.webservicex.net/TranslateService.asmx?wsdl
Result: It will generate bunch of .class files under ./TranslatorWS folder

3. Generate a jar file from the generated .class files

Open a terminal and navigate to your project.
Run on terminal:
$ jar cvf TranslatorWS.jar -C ./TranslatorWS/ .
Result: A jar file name TranslatorWS.jar is generated out of the dynamic proxy of the remote Web Service.

4. Write Client code

 Let's structure our code by creating ./src folder and write something like com.client.TranslatorWSClient.java


(Note: Inside src is com folder. Inside com is client folder. Inside client folder is the Java class)
Result: You are able to create the code to utilized the web service.




package com.client;

import net.webservicex.Translate;
import net.webservicex.TranslateService;
import net.webservicex.TranslateServiceSoap;

public class TranslatorWS {
/**
* * @param args
* */
public static void main(String[] args) {
if (args.equals(null) || args.length == 0){
printHelp();
}

TranslateServiceSoap translatorService = new TranslateService().getTranslateServiceSoap();
if ("EnglishTOFrench".equals(args[0])){
System.out.println("Translation:");
System.out.println(translatorService.translate(args[0], args[1]));
}
}
private static void printHelp() {
System.out.println("TranslatorWS <language> <Word To Translate>");
}
}


5. Compile Client Code

Create a directory ./bin where you will store your compiled .class client class.
RUN on terminal:
$ javac -classpath ./TranslatorWS.jar -d ./bin/ -verbose ./src/com/client/TranslatorWS.java
Result : compiled client code under ./bin directory.

6. Run the client to utilize remote web service

Run on Terminal:
$ java -classpath ./bin/:./TranslatorWS.jar com.client.TranslatorWS EnglishTOFrench goodTranslation:
Error occured when translating text please contact support@webservicex.net
Result: A translation of the word. However, on this web service, an error message is sent back instead of the real thing.


NOTE: These are free Web services just made for practice or tutorials therefore they are unreliable, sometimes their response time is quite big and wrong.