Below steps explains the how to create a web-service in java in bottom-up approach using the STS(Spring tool suite) IDE. In the bottom-up approach, first we will create a template class, using the template class we will generate the WSDL and deploy the service in servers. Follow the below steps to create a webservice.
Step 1: Create a new java project using the steps mentioned here
Step 2 : Create a template Java class as service
Create a Java class in src folder of the in.javatutorials package as below
Provide the package name and class name into respective fields and click on finish button.
Implement the Java methods as give below:
Use the below source code to write the class:
package in.malliktalksjava;
/**
* Calculator class exposed as a webservice
* @author malliktalksjava
* @since Version 1.0
*
*/
public class Calculator {/**
* adds the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer addition(int var1, int var2) {Integer result = var1 + var2;
System.out.println(“addition result in service : ” + result);
return result;
}/**
* multiply the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer multiplication(int var1, int var2) {Integer result = var1 * var2;
System.out.println(“multiplication result in service : ” + result);
return result;
}/**
* divide the two input parameters and return the result
* @param var1
* @param var2
* @return
*/
public Integer division(int var1, int var2) {Integer result = var1 / var2;
System.out.println(“division result in service : ” + result);
return result;
}}
Step 3: make the java class as web service
Select the Java class as below
Choose Web Service from the and click on Next button
Select the ‘Bottom up Java bean Web Service’ from the Web service type and service implementation class as mentioned in the below picture and click on Next button.
Select the service methods from the menu and click on Finish button. The Service implementation style used is document/literal as shown in below picture.
The folder structure of the web application looks like below:
Deploy the service into tomcat web server and access the WSDL file using the below url:
http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl
Finally your web service is ready to use and WSDL url is the end point url for your web service.
Other Useful links:
Click here to know more about webservices
Click here to know more about RESTfull web services.
Click here for Web services Question and Answers.
Click here to know how to write web service client suing java.
Filed under: Web Services Tagged: creating a java web service, creating a web service using java, how to create a web service in java using STS, how to create a webservice in java using eclipse, Java, Java Web services, Service-oriented architecture, Web service, Web Service Description Language, WSDL