Create a Java web service using top down approch


In the bottom up approach, we will write the java class and generates the WSDL file and other dependent components. The same will be deployed into the web containers.

In Top down approach, Architects will write the WSDL file based on the requirements. Developer need to make the corresponding service implementation using the WSDL provided. This post will explain how to create a service using the WSDL file.

Step 1: Create a dynamic or java project as mentioned here

Here, I have created a sample web dynamic project with the name SampleWS as given below.

Dyanmic web project

Step 2: generate the service using top down approach

Right click on the SamplWS project name -> New -> Other

SelectOther

Select the Web Service from the wizard as below and click on Finish button.

select webservice

Select the Web service type as ‘Top down Java bean Web service’ and provide the WSDL url in the Service definition drop down and click on Finish button.

Sample WSDL URL is: http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl

providethewsdl

Your Web service is ready with the Java bean methods as below and the Final folder structure looks like below:

service Folder structure

Write the business logic into the Service class as given below:

Generated class:

/**
* CalculatorSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/

package in.malliktalksjava;

public class CalculatorSoapBindingImpl implements in.malliktalksjava.Calculator{
public int addition(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}

public int multiplication(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}

public int division(int var1, int var2) throws java.rmi.RemoteException {
return -3;
}

}

Implemented class:

/**
* CalculatorSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/

package in.malliktalksjava;

public class CalculatorSoapBindingImpl implements in.malliktalksjava.Calculator{
public int addition(int var1, int var2) throws java.rmi.RemoteException {
return var1+var2;
}

public int multiplication(int var1, int var2) throws java.rmi.RemoteException {
return var1*var2;
}

public int division(int var1, int var2) throws java.rmi.RemoteException {
return var1/var2;
}

}

Deploy the application into server and use the below url as a WSDL for this to have the client.

http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl

 

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.

Write a Client for web service


Below steps explains how to write a web service client in java using STS IDE.

Step 1: Create a Java project using the steps mentioned here.

Step 2: Generate the stubs for the Java web service using below steps

Mouse Right click on Client project and select New -> Other

select other option

Select the Web service client from the wizard

Select webservice cliet

Provide the service WSDL url in the Service Definition text box and click on finish button.

Enterwsdl into service defination

Web service client stubs will be generated into the package and final folder structure looks below.

Client stubs generated

Write the Client class using the stubs and test the client project.

Write a client

 

Use the below sample code to write the client:

 

package in.malliktalksjava.client;

import java.rmi.RemoteException;

import in.malliktalksjava.Calculator;
import in.malliktalksjava.CalculatorServiceLocator;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

/**
* @author Javatutorials
* @since version 1.0
*
*/
public class SampleWSClient {

/**
* @param args
*/
public static void main(String[] args) {

SampleWSClient sc = new SampleWSClient();
sc.callCalculatorWebservice();
}

/**
* used to call web service
*/
public void callCalculatorWebservice(){

String wsdl = “http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl”;
QName queue = new QName(“http://malliktalksjava.in”, “CalculatorService”);

try {
//create the servicelocator object
CalculatorServiceLocator calServiceLoc = new CalculatorServiceLocator(wsdl, queue);
//create the service object
Calculator calculator = calServiceLoc.getCalculator();
//call the service methods
System.out.println(“addition result : “+calculator.addition(10, 11));
System.out.println(“division result : “+calculator.division(10, 5));
System.out.println(“multiplication result : “+calculator.multiplication(10, 10));
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

 

With this your client application is ready to use.

 

Other Useful links:

Click here to know how to create the web service project.

Click here to know the difference between SOAP and RESTfull web services.

Create a java webservice using STS


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

Writeaclass

Provide the package name and class name into respective fields and click on finish button.

Namethetheclass

Implement the Java methods as give below:

write the class

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

maketheclassas service

Choose Web Service from the  and click on Next button

createservice

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.

bottomup webservice

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.

choose service methods

The folder structure of the web application looks like below:

webservice folder structure

Deploy the service into tomcat web server and access the WSDL file using the below url:

http://localhost:8080/SampleWebService/wsdl/Calculator.wsdl

wsdlfile

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.