Create Web Service in Java

Web Service is a method that is used for the purpose of exchanging the data with other web application via the web or cloud using HTTP,XML, SAOP etc. A main advantage of this is that it allows for communicate data between different languages. For Example, we can write our application in any language like Java then access it via a web service using PHP or .Net.

What is wsdl?

WSDL means Web Service description language. This is an XML-based language that provides complete description of web service.WSDL document contains the different elements that are:
Definition: It is the root element of the document. It defines the name of the web service
Data types: Define the data types used by the web service in the form of XML Schema
Message:It defines the data elements for each operation
Operation: It is the abstract definition of the operation for a message, such as naming a method.
Port Type: It described the operation that can be performed and the message involved
Binding: It is the protocol and data format for a particular port type.
Port: It is a combination of a binding and a network address, providing the target address of the service communication.

What is SOAP?

There are two ways for accessing the webservice One is Simple Object Access Protocol(SOAP) and second is Representational state transfer(REST).
Creating and deploying webservice in java using NETBeans Framework.

1.Create Web Application

Firstly download the netbeans from link given below:

Download Netbeans
  • After installation firstly open the Net beans then Go to File > New Project
  • Select the Java Web from category and select the web application from Project's.
  • After that give the project name like WEbServiceCre.
  • Now Select which server do you want to use either Glassfish server or Tomcat.
  • After than press next and than Click on Finish button.
  • Then the default web application named WEbServiceCre is created successfully.

2.Create Web Service

In project window select your project named WEbServiceCre. After that right click on source package than create new Java Package from list that’s name is abc.
After creating the package than right click on package and create java class from list that’s name is SimpleWebService.
Now create the public method in the class with th following code:

public String sayHello(String name){
   return "Hello " + name + "!";
}

Now our class look like this:

package abc;
public class SimpleWebService {
  public String sayHello(String name){
        return "Hello " + name + "!";
     }
}

This is a simple java class. So now if we want to turn this in webservice than add annotation in this class. Add annotation @ WebService above the class and @WebMethod above the method and import their classes for using these annotation i.e.

import javax.jws.WebMethod;
import javax.jws.WebService;

Now our web service look like that

package abc;
import javax.jws.WebMethod;
import javax.jws.WebService;
@ WebService 
public class SimpleWebService {
@WebMethod
  public String sayHello(String name){
        return "Hello " + name + "!";
     }
}

3.Deplyoing the webservice

Now right click on the project and select “Clean and Build”
After the projects been built, right click it again and select “Deploy”
The IDE will now launch an instance on apache server and put the webservice on it. After that open the server by using localhost:8080 and check the webservice output by click on the WEbservicecre application.