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.

What are the differences between SOAP WS and RESTful WS?


SOAP Web Services RESTfull Web Services
The SOAP WS supports both remote procedure call (i.e. RPC) and message oriented middle-ware (MOM) integration styles. The Restful Web Service supports only RPC integration style.
The SOAP WS is transport protocol neutral. Supports multiple protocols like HTTP(S),  Messaging, TCP, UDP SMTP, etc. The REST is transport protocol specific. Supports only HTTP or HTTPS protocols.
The SOAP WS permits only XML data format. You define operations, which tunnels through the POST. The focus is on accessing the named operations and exposing the application logic as a service. The REST permits multiple data formats like XML, JSON data, text, HTML, etc. Any browser can be used because the REST approach uses the standard GET, PUT, POST, and DELETE Web operations. The focus is on accessing the named resources and exposing the data as a service. REST has AJAX support. It can use the XMLHttpRequest object. Good for stateless CRUD (Create, Read, Update, and Delete) operations.GET – represent()POST – acceptRepresention()

PUT – storeRepresention()

DELETE – removeRepresention()

SOAP based reads cannot be cached. REST based reads can be cached. Performs and scales better.
SOAP WS supports both SSL security and WS-security, which adds some enterprise security features like maintaining security right up to the point where it is needed, maintaining identities through intermediaries and not just point to point SSL only, securing different parts of the message with different security algorithms, etc. The REST supports only point-to-point SSL security. The SSL encrypts the whole message, whether all of it is sensitive or not.
The SOAP has comprehensive support for both ACID based transaction management for short-lived transactions and compensation based transaction management for long-running transactions. It also supports two-phase commit across distributed resources. The REST supports transactions, but it is neither ACID compliant nor can provide two phase commit across distributed transactional resources as it is limited by its HTTP protocol.
The SOAP has success or retries logic built in and provides end-to-end reliability even through SOAP intermediaries. REST does not have a standard messaging system, and expects clients invoking the service to deal with communication failures by retrying.

 

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 create a SOAP web service.

Click here to know how to write web service client suing java.

WEB Services Interview Questions


1)What is web Services ?
Getting the services through the web.

2)What is JAX-RPC?
JAX-RPC means java api for xml based RPC is a java api for developing and using web-services.
An RPC based web-service is a collection of procedures that can be called by remote client over the internet.
Ex: A typical RPC based web service is a stock quote service that takes a SOAP request for the price of a specified stock and returns the price via SOAP.

3)Explain about Web Services
A Web service, a server application that implements the procedures that available for clients to call, deployed on server-side container.
A web service can make itself available to potential clients by describing itself in a Web Service Description Language (WSDL) document.

4)What is WSDL?
WSDL is an XML document that gives all pertinent information about web service, the operation that can be called on it, parameters for those operations, and the location of where to send requests.
A web client can use WSDL document to discover the what the service offers and how to access it.

5)Explain some of the features of the web services?
Some of the features of the web services are:
1.Interoperability
Using JAX-RPC, a client written in a language other than Java programming language can access a web service developed and deployed on the Java platform. Conversely,a client written in the Java programming language can communicate with a service that was developed and deployed using some other platform.
2.Ease of use:
Using JAX-RPC makes the user to feel comfortable over the RPC. The internal complicated infrastructure, like marshaling, unmarshaling, and transmission details..etc, will be taken care automatically.
3.Advanced features

6)What makes interoperability is possible?
JAX-RPC support for SOAP and WSDL.

7)What is JAXM?
Java API for Messaging provides a standard way to send XML documents over the Internet from the Java platform. It is based on SOAP 1.1 and SOAP with attachments specifications, which define a basic framework for exchanging XML messages.
8)The features of JAXM over the RPC including JAX-RPC
a) One way(asynchronous)messaging.
b) Routing of message to more than one party.
c) Reliable messaging features such as guarantied delivery.

9)What is JAXR?
Java API for XML registries provides a convenient way to access standard business registries over the Internet. Using JAXR one can register the business registry and one can search for a business registry for other business.

10) What tools do you use to test your Web Services?

 SoapUI tool for SOAP WS and the Firefox “poster” plugin for RESTFul services.

11) What is the difference between SOA and a Web service? 

SOA is a software design principle and an architectural pattern for implementing loosely coupled, reusable and coarse grained services. You can implement SOA using any protocols such as HTTP, HTTPS, JMS, SMTP, RMI, IIOP (i.e. EJB uses IIOP), RPC etc. Messages can be in XML or Data Transfer Objects (DTOs).

Web service is an implementation technology and one of the ways to implement SOA. You can build SOA based applications without using Web services – for example by using other traditional technologies like Java RMI, EJB, JMS based messaging, etc. But what Web services offer is the standards based  and platform-independent service via HTTP, XML, SOAP, WSDL and UDDI, thus allowing interoperability between heterogeneous technologies such as J2EE and .NET.