Java WebService - Create your first web service in Java
By: Emiley J in WebServices Tutorials on 2013-07-09
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; // time server import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; /** * The annotation @WebService signals that this is the * SEI (Service Endpoint Interface). @WebMethod signals * that each method is a service operation. * * The @SOAPBinding annotation impacts the under-the-hood * construction of the service contract, the WSDL * (Web Services Definition Language) document. Style.RPC * simplifies the contract and makes deployment easier. */ @WebService @SOAPBinding(style = Style.RPC) // more on this later public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed(); }
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(); } }
Now you can check whether everything is fine by compiling.
First go to the folder above javasamples
Next compile the two files by executing this command ( javac javasamples/one/*.java )
If there are no errors then congratulate yourself. You have just developed a webservice that returns the system time. Its a time server.
If you get a compile error, then check this solution for package javax.jws does not exist
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.
Normally, in a production system, you will publish the web service in a server such as GlassFish, Jboss, Websphere etc.. But while testing and in development, we will just create a simple java application that will publish this web service in localhost.
Go to the javasamples/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, an arbitrary name. * The Endpoint class has an overloaded publish method. * In this two-argument version, the first argument is the * publication URL as a string and the second argument is * an instance of the service SIB, in this case * ch01.ts.TimeServerImpl. * The application runs indefinitely, awaiting service requests. * It needs to be terminated at the command prompt with control-C * or the equivalent. * Once the applicatation is started, open a browser to the URL * http://127.0.0.1:9876/one?wsdl * * to view the service contract, the WSDL document. This is an * easy test to determine whether the service has deployed * successfully. If the test succeeds, a client then can be * executed against the service. */ public class TimeServerPublisher { public static void main(String[ ] args) { // 1st argument is the publication URL // 2nd argument is an SIB instance Endpoint.publish("http://127.0.0.1:9876/one", new TimeServerImpl()); } }
Now you can check whether everything is fine by compiling.
First go to the folder above javasamples
Next compile the two files by executing this command ( javac javasamples/one/*.java )
If there are no errors then congratulate yourself. You have just developed a webservice that returns the system time. Its a time server.
If you get a compile error, then check this solution for package javax.jws does not exist
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 http://127.0.0.1:9876/one
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Content-Security-Policy: object-src, script-src, base-uri and report-uri
Preventing your PDF files to be displayed inside other website IFrames
Prevent other websites from displaying content from your website in an iframe
Returning multiple values from a web service
Java Webservices using Netbeans and Tomcat
How to Deploy a Java Web Service
Java WebService connected to Database
package javax.jws does not exist
Comments