How to Create Web Service in Java?

There are two ways to create web service either from java code or WSDL file. If you want to create web service application is to start with java coding. If you want to develop the Java web services either by scratching or via existing java class you need to expose the web services as this is the only approach. Here you can check “How to create Web Service in Java?”

Web Service in Java

The API of the java for XML web services mainly relies on the annotations specifies Metadata Facility for Java language. Initially, you need to set up the server. The server allows you to host the local server via own machine. In fact, with NetBeans, you can install the server or download the server whenever you have created the project.

Create a Web Service

  • Press the right click on the Source packages
  • Visit the New folder
  • Select the Java class list
  • Choose the web service
  • It will generate the web service class automatically.

Here you can check the example:

Firstly, you need to create the public method inside the new public method with the code mentioned below.

public String sayhello(String Name){

return “Hello”+ name+ “!”;

}

Then you need to create a class that looks like

Public class SimpleWebService {

Public String sayHello(String name){

Return “Hello” + name + “!”;

}

}

The final layout should look like other java class for which you need to add annotation @WebService and @WebMethod. Check the layout here

Import javax.jws.WebMethod;

Import javax.jws.webservice;

@webservice

Public class simplewebservice {

@WebMethod

Public String sayhello(String Name){

Return “Hello” + name+”!”;

}

}

How to create the project with NetBeans?

Deploy web service

  • Select the file and then go to the new project icon.
  • Choose the Java category and on the other hand choose the web application.
  • Give the name of your project and enter the next icon.
  • Now from the list, you need to choose the server that you want to use.
  • If you will not find any server then choose the Add button and choose Glassfish.
  • Press the next icon to download the server.
  • Click on finish icon.
  • You will find a page with the name as index.html. If you do not require it then you can close it by clicking on the “x” button.

Deploying the Web Service

Now the web servers have created and check whether the server is actually working or not.

  • Press the right click on the project and choose the “Clean and Build” option.
  • If your project has been completely built then press the right click on it.
  • Choose the “Deploy” option.

Leave a Reply

Your email address will not be published. Required fields are marked *