Thursday, August 21, 2014

Java SOAP Web Service (1)

What is a SOAP Web Service?
SOAP stands for Simple Object Access Protocol. This protocol allows a service to be running on a remote machine and accessed not only from another computer but from different languages as well. For example you can write a Java Web Service and someone else can be writing a C++ application that uses your service.

How is this possible?
There is no good way to go straight from a POJO (Plain Old Java Object) to a C++ object, so what happens is the web service and the client have a common component called the SEI (Service Endpoint Interface) and this SEI is responsible for converting the POJO to a generic type and the client SEI is responsible for converting from the generic type to C++ (in this example). The generic protocol I am referring to is XML!

How do I make a SEI?
Easy! The SEI can be auto-generated for you by using the WSDL for the web service. After setting up JavaEE SDK you will have a tool available to you called 'wsimport' from command line that will allow you to generate the SEI automatically.

  1. Open your Terminal or Command Line
  2. Type 'wsimport -keep -d <directory to put files> <WSDL URL>
  3. Now you can copy the .java files in this case into your Eclipse project and call their methods defined by the Web Service
How to create a WSDL?
The WSDL or Web Service Description Language is commonly referred to as the 'contract' and it contains all of the type information, method calls, and objects template for the web service.
As the developer of the Service you have it pretty easy:

  • import javax.jws.WebService;
  • Add @WebService to the class or interface
    • Optional: Add arguments to the annotation
    • Optional: Mark methods with @WebMethod annotation (public methods will be publish by default)
  • Publish it to Glassfish

If you have any further questions about what a SOAP Web Service is ask in the comments section.
For instructions on how to install JavaEE SDK and configure Glassfish Server see my related blog post.



Installing JavaEE SDK 7_u45 and Glassfish

This guide is a walkthough of how to install Java EE SDK and Glassfish server to your computer. My images show the steps for Windows, however, the steps for other Operating Systems are very similar:
If you just want to install basic Java and do not need the Developers version see this website.

  • If using Linux:
    • Make sure when installing the Glassfish Server that you install to a directory that you have permission to.
    • Do not forget to set your JAVA_HOME if it is not done automatically. For help with that check out this example and make sure to replace the Java version with the appropriate version.
  • Mac OSX
    • Same as Linux, but use this example for setting $JAVA_HOME

  1. Go to Oracle's website and under the "Downloads" Menu option select "Java for Developers"
  2. Select the "Java EE" option from the left-hand menu and then choose the type of EE installation you need, I chose the version with Glassfish included, you do not have to.
  3. Make sure to accept the license agreement on this page (in the grey box above downloads) or else you will not be able to download the required files. Once again I am showing images for Windows installation.
  4. When the .exe has completed it's download you can click on it and allow the installation to start. Unless you want to configure the install properties manually, using the "Typical Installation" option is recommended. After pressing 'next' you will have the option to configure the installation directory, by default on Windows it is set to "C:\glassfish". Make sure that the option for the Update Tool is checked when you get that far.
  5. Once you get to the final screen of the installation wizard you can verify that the installation of the Glassfish Server was successful by opening your browser of choice and entering the following URL "http://localhost:4848" and pressing enter. If Glassfish was installed properly you will be directed to the Admin Console. (Specifying the '4848' after localhost: is the port that the Glassfish server is running on)

That's it! You have installed the JavaEE SDK and set up Glassfish Server. I will be making a tutorial on how to configure Glassfish Server in Eclipse(JavaEE kepler) and then how to create a simple Java Webservice (SOAP) and deploy it to Glassfish.