ADVERTISEMENTS

Create Web Service in Java

Nitasha BatraFeb 27, 2018
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.

ADVERTISEMENTS

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 click on New Project under File Option
  • Select option i.e. Java Web from category and select the web application from Project's.
  • After that give the project name like WEbServiceCre.
  • Now choose server which do you want to use either Glassfish server or Tomcat.
  • After that click on next button and then on Finish button.
  • Then the default web application named WEbServiceCre is created successfully.

2.Create a new 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 the code given below:

public string callHello(String name){
   return "Hey Buddy " + 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 + "!";
     }
}
<input type="hidden" name="IL_IN_ARTICLE">

 

ADVERTISEMENTS

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.

ADVERTISEMENTS