Java WebService - Create your first web service in Java
By: Emiley J Emailed: 1676 times Printed: 2150 times
If you are tasked to develop a web service in Java, you will probably try to google around and check how to get started. If you are a beginner in Web Services, you will surely get confused with the various tools, terms, acronyms, technologies related to Java based web services. In reality, it is NOT that complex. The problem usually lies in the fact that there is no complete tutorial for a beginner. Of course you can find some on the web, but they normally tend to use complex acronyms and steps that will confuse the beginner. Therefore this tutorial will help you to create your first Java web service using just the command prompt and JDK.
Here are the pre-requisites that you need to have.
- Download and install the latest JDK (jdk 1.7)
- Create two environment variables in your system JAVA_HOME -> Point to your jdk installation folder. In PATH environment variable, if you have previous jdk paths, remove it and add the
/bin
Now open the command prompt and create a folder named "javasamples" ( md javasamples )
Next cd javasamples to goto that folder. Now create another folder named "one" ( md one )
Next create a file named TimeServer.java ( notepad TimserServer.java )
Copy the below code in that file and save it.
package javasamples.one; //();
}
Next create a file named TimeServerImpl.java ( notepad TimserServerImpl.java )
Copy the below code in that file and save it.
package javasamples.one;
import java.util.Date;
import javax.jws.WebService;
/**
* The @WebService property endpointInterface links the
* SIB (this class) to the SEI (javasamples.one.TimeServer).
* Note that the method implementations are not annotated
* as @WebMethods.
*/
@WebService(endpointInterface = "javasamples.one.TimeServer")
public class TimeServerImpl implements TimeServer {
public String getTimeAsString() { return new Date().toString(); }
public long getTimeAsElapsed() { return new Date().getTime(); }
}
Wait, thats not all. You have developed the webservice alright. But you need to publish it right? If you don't publish it, then nobody will be able to find it and consume it. Therefore the next step is to publish your service./one folder.
Now create a new java file named TimeServerPublisher.java ( notepad TimeServerPublisher.java )
And copy the below code into it.
package javasamples.one;
import javax.xml.ws.Endpoint;
/**
* This application publishes the web service whose
* SIB is javasamples.one.TimeServerImpl. For now, the
* service is published at network address 127.0.0.1.,
* which is localhost, and at port number 9876, as this
* port is likely available on any desktop machine. The
* publication path is /one,
Finally, you need to start your TimeServerPublisher class. You can do this like ( java javamples.one.TimeServerPublisher ). That't It. You have created your first java web service and publish it. You can double check whether it works by opening your brower and going to