Sunday, September 18, 2016

Creating sample webservice in Java

In this article, I will be explaining the steps to be followed to create a simple webservice using Eclipse. We will be using Eclipse Luna version to create the webservice.

In this example, we will create a webservice which will return "Hello World" text to the client.

The steps are given below.

1. Go to New -> Project option in eclipse.



2. From the select wizard displayed, select the option web -> Dynamic Web Project. Click Next



3. Type in a project name. In this example, we have given the project name as "HelloWorldWebService". Click Next


4. In the upcoming pages also, select the default options and proceed. From the below page, click "Finish".



5. Now a project with the given name will be created in Eclipse. Expand the "Java Resources" section and right click on "src" folder under it.




6. Choose New -> Classes option. In this example, the name of the class is mentioned as "HelloWorld" and package name as "com.test.service".


7. Write the code snippet as shown below in the newly created class.

package com.test.service;
public class HelloWorld {

 public String getHelloWorld() {
  return "Hello World";
 }
}


8. Now, right click on the class name and select WebServices -> create webservice option as shown below.




9. Click the checkbox "publish the webservice". Press next button.



10. By default the method that we have created in the Java class will be displayed. Make sure that the checkbox is selected. Click Next



11. In order for the webservice to be accessible by the clients, the server should be started and the service should be published in the server. From the below screen, click on "Start server" and then click "Finish".




12. The web service is ready now. The below screen will be displayed in Eclipse which will provide the endpoint details and parameter and response details.

It's very simple. right?

 

No comments:

Post a Comment