Pages Navigation Menu

Coding is much easier than you think

JAX-WS Web services using Eclipse

Posted by in Web Service

JAX-WS-Hello-world
 
In this article we shall learn to implement JAX-WS Endpoint and Client using eclipse.
 
Note: In general words, “web service endpoint” is a service which published outside for user to access; where “web service client” is the party who access the published service.
 

Environment Used

 
JDK 7 (Java SE 7)
Eclipse JUNO IDE
 

JAX-WS Web Service End Point

 
Create a java project(File -> New -> Java Project) “JAXWsServer” with the following folder structure.
 
Project Structure
 

WS-Service Endpoint Interface

 
File: Square.java

package com.webServices;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface Square {

	@WebMethod
	public int square(int num);

}

 

WS-Service Endpoint Implementation

 
File: SquareImpl.java

package com.webServices;

import javax.jws.WebService;

@WebService(endpointInterface = "com.webServices.Square")
public class SquareImpl implements Square {

	public int square(int num) {
		return num * num;
	}
}

 

Endpoint publisher

 
File: SquareWSPublisher.java
 

package com.webServices;

import javax.xml.ws.Endpoint;

public class SquareWSPublisher {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8091/WS/Square", new SquareImpl());
	}
}

 
Run the endpoint publisher, by right clicking on the above program -> Run As -> Java Application, now your “Square web service” is deployed in URL http://localhost:8091/WS/Square.
 

Test It

 
You can test the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL http://localhost:8091/WS/Square?wsdl.
 

Developing Web Service Client

 
Create a new java project and name it JAXWSClient
 

Client stubs

 
To generate the client stubs you need to open the command prompt and enter the following wsimport command

CD %CLIENT_PROJECT_HOME%src
wsimport - keep http://localhost:8091/WS/Square?wsdl

 
After running the above command, refresh your “JAXWSClient” project in eclipse, Now you will find java classes generated under src folder of “JAXWSClient” project.
 
JAX Client Structure
 
Where is wsimport?
This wsimport tool is bundle with the JDK, you can find it at “JDK_PATH/bin” folder.
 

Client Class

 
Lets create Client Class which consumes the web services.

package com.client;

import com.webservices.Square;
import com.webservices.SquareImplService;

public class Client {

	public static void main(String[] args) {
		SquareImplService service = new SquareImplService();
		Square square = service.getSquareImplPort();
		System.out.println("Square of 10 is- " + square.square(10));
	}
}

 

Run it

Now right click on the above program -> Run As -> Java Application and you will get following output.

Square of 10 is- 100

 
That’s all on how to create a JAX-WS Endpoint and Client in eclipse.
 

Read More

Create and Deploy Web Service and Web Service Client in Eclipse

Posted by in Java, Web Service | 2 comments

Web-Services
 
In our previous tutorial, we got introduced to Web Services and the Concept associated with web service technology. In this article we shall learn to Create and Deploy Web Service and Web Service Client in Eclipse.
 
There are two ways to develop a web service namely top-down approach and bottom-up approach. To know about these approaches and in general about web service refer my Concept associated with web service article. In this article we will be using bottom-up development approach.
 

Environment Used

 
JDK 7 (Java SE 7)
Eclipse JUNO IDE
Apache Tomcat 7.x
 
Just for your information, Eclipse by default uses Apache Axis to implement the web service and it provides option to use our choice of web service engine. I decided to go with the default bundled Apache Axis.
 
Initially Install Eclipse IDE and configure apache tomcat in it, and Create a dynamic Web Project with the following configuration
 
creating-dynamic-web-project-eclipse
 
Now create a simple calculator java program as with the code below
 
File: Calculator.java

package com.webServices;

public class Calculator {
	public int add(int a, int b) {
		return (a + b);
	}

	public int subtract(int a, int b) {
		return (a - b);
	}

	public int multiply(int a, int b) {
		return (a * b);
	}

	public int divide(int a, int b) {
		return (a / b);
	}
}

 

Initial Project Structure

The initial project structure of application should be as shown below
 
Initial project structure
 
Now Right Click on file Calculator.java -> Web Services -> Create Web Service and Select options as mentioned in below diagram and Click finish.
 
Create-Web-Service-from-Java-Class-in-Eclipse
 
Here we are instructing Eclipse to generate a web service and its client. These steps will create a new dynamic web java project WebServicesClient .
 

Final project structure:

 
Final Project Structure
 
Both WebServices and WebServicesClient projects will be automatically deployed to server. Also, Eclipse automatically opens Web Service Test Client Window with URL: http://localhost:8089/WebServicesClient/sampleCalculatorProxy/TestClient.jsp?endpoint=http://localhost:8224/WebServices/services/Calculator
 
Now click on add(int,int), subtract(int,int), divide(int,int) ,multiply(int,int) on the Web Service Test Client Window and provide an input to check updated result.
 
WebService-Client-Invoke-Result-Page-in-Eclipse
 
That’s all on how to Create and Deploy Web Service and Web Service Client in Eclipse.
Happy learning :)

Read More

How to choose between SOAP Web Services and RESTful services?

Posted by in RESTful Services, Soap Web Service, Soap-UI, Web Service | 2 comments

 
webservices
 

Choose SOAP:

  • If you require asynchronous processing
  • In your service operations are statefull
  • If you need formal contract/Interfaces

 

Choose RESTful Services:

  • If you have limited bandwidth
  • If your clients require caching.
  • If your operations are stateless

 

Read More

Concept associated with web service technology

Posted by in Soap Web Service, Web Service | 4 comments

Web-Services
 
In last article we learned about what web services is. In this tutorial we will try to understand some of the concepts associated with web services technology.

Let stat with the scenario, let us say I’m writing a java implementation class, if I want to share this implementation with other consumer class, then the best way to share the implementation class is through the interface
 

Normal web application

So Interface is a standard way in which you can share any contract (implementation class) to a consumer.

so now think about how this could work in case of web service

Now let’s say I have a web service implementation, I want to share the details of web service to the consumer through an interface,

Web service demo

In this case the consumer can be in any language and so the web service, So the interface that we are going to share with the web service consumer must be technology independent, it should be something that any technology can understand, so the concept of interface will not work here.

In order to cater to the need for a platform independent format for interaction, XML format was adopted for this purpose. Now when we create a web service, we will share its contract(implementation) as XML document.

This XML is called WSDL

Web service

WSDL stands for Web Service Definition Language or Web Service Description Language.

WSDL document will contain the contract of the web service. So when we create a web service we will share the WSDL to the consumer class.

WSDL for an existing web service can be generated either manually or automatically (through available tools)

Contents of WSDL:

1. Methods

2. Argument

3. Return type.

This is a high level representation of WSDL.

Therefore through WSDL we will get to know what information needs to be sent and its return type.

Now let us say that you are going to create your own application and you are about to call a web service

Now there occurs a question, how consumer is able to locate WSDL for a particular Web Services?

As we know that the one way to get web service information is through the WSDL, but now how do we get hold of the WSDL?

Solution : Here there are 2 solutions for this,

  1. If Web Services knows the consumer, then it can directly give the WSDL to the consumer establish web services connection. Web service WSDL direct method
  2. There is an registry called UDDI on internet. It is a directory, where all service providers who want to issue their web services would 1st need to register the web services with UDDI using WSDL and make themselves available through it for discovery.
     

Now consumer who is looking for a particular service would go and hit UDDI, and will search for a particular service that it is looking for. Now UDDI will return all service providers who offer that service. And then Consumer will choose one service provider from it, and get its WSDL, now with the help of WSDL, consumer will able to access web services.
 

UDDI stands for Universal Description Discovery and Integration.

 
Web service through UDDI
 
Now we got the WSDL, so we know what information needs to be sent and its return type.

Now let us assume that you are writing a client application (in java) to call a web service (in C++), so now we can get the WSDL by querying the UDDI.
 
description soap web service
 
But now the question is how do we actually send the information and how the exchange happens?
 
The parameter sent from our consumer class (say, some complicated object) might not be recognized by the other language in the web service which we are about to call. Thus, the object passed will make no sense to the web services and hence the web services will not be able to process the input parameter.
 
For example I have a java String as input parameter which is needed to be sent to web service which is written in C++.
As we know a string in java is different from string in c++. So how do we exchange the data between client application and web server?
 
So the information passed (input arguments) and the information received (return types) should be in a common format. Again XML format comes to the rescue.
 
There is a protocol by which both sender and receiver understands this format (XML).

This protocol is known as SOAP.
 

SOAP stands for Simple Object Access Protocol.

 
SOAP Protocol in web service
 
SOAP is a way by which different technology can access objects.

 
Now you know what a web services is and you know what needs to be sent (WSDL) and how to send it (SOAP).
 
But now who does the conversion?
 
Referring to the above assumption, how to do the conversion from java objects to SOAP message takes place?
 
Well this conversion is done by an intermediate class in the client application which is SEI.
 
SEI in web service
 
SEI stands for Service Endpoint Interface.

 
SEI acts as an interface to your web service endpoint. SEI takes care of converting client object to a SOAP message. And we don’t need to write this class ourselves, we can generate it automatically.

Note:When you are about to make a web service call, you don’t need to worry about where the web service is and where you need to call. All you have to do is to have this SEI generated/Created and you can call the method of SEI.

One good thing about SEI is that, you can actually have interface specific to what you are developing.

For example, if my client is in java application, then I can have a SEI specific to java application, which knows to convert java objects to soap objects. Similarly for dot net, SEI will be in dot net, which converts dot net code to SOAP messages.

I.e. SEI takes care of all web service call.
 
SEI for different platform

Web Service Design

 
There are two approaches in implementing a web service and they are bottom-up and top-down.
 

Bottom Up Approach

 
Bottom up approach is where we first define the logic of a web service and then using that we will build the WSDL file. There are tools available to generate the wsdl file automatically based on it.
Refer: Create and Deploy Web Service and Web Service Client using bottom up approach
 

Top Down Approach

 
Top down is the reverse of bottom up approach. First the business logic is written up and then we create the WSDL. The complete business definition, message format, transport protocol, security and everything will be described in WSDL. Using that wsdl the skeleton code is generated automatically.
 
Summary
 
In this tutorial we have learnt 4 terminologies so far.

1. WSDL – describes what a web service is in an XML format.

2. UDDI – it is a directory where any publisher can publish their web services and a consumer can query and get access to all different web services.

3. SOAP – it’s basically a protocol that is used to encode and decode messages. When you make a call to web service, it ends up as SOAP message that gets transferred over the network.

4. SEI – it’s an interface to the service endpoint (web service), that provides a way for client application irrespective of the technology to call the web service. Depending on the technology you get a SEI for that technology. This can be generated out of the WSDL itself.
These are some of the important concepts about web service. We will see some more detail in the next tutorial.
 

Read More

Introduction to Web Services

Posted by in Soap Web Service, Web Service | 1 comment

webservices
 
In this article we will get an introduction about what an web services is. In case of web service there are primarily two different types

  1. SOAP Web Service
  2. RESTful Web Service

The java specification for SOAP IS JAX-WS and for REST is JAX-RS

 
In this tutorial we will concentrate about learning SOAP Web Service.
 
Now what is a web service?

Basically, we can say that everything available online are services. Difference between a standard website and a Web Services is, a website stands for human consumption but a Web Services stands for code consumption.

Let us try to understand that better..

Imagine your friend has an existing application which takes a particular day as input and returns the events that take place on that specific day. Lets say it has a method getEvents() which takes the input date and returns a List of Strings containing the events. For a general website, this application is present in MVC form and  is deployed in an application server. This way, the functionality is available to the end users who can send their input to the application and get their desired result.

Suppose I have a completely different App server, and I wants to use the getEvents() functionality of my friend’s application into mine.

Then one way I can do it by  Getting jar of original server.

 The disadvantage of this method is,

1. My friend’s application may have different application server, database and have other business which I’m not interested in.

2. If my friend changes the jar, then I need to get it and build it again.

Then Solution of the above problem is to use  Web Services

Web Services allows two different applications running on different machine to communicate with each other. But there should be a network connecting these two applications.

The best part of Web Services is inter operability, i.e. java can call .net and vice versa

Illustration:

Web service introduction

In the above diagram, application (A1) calls a function in application (A2).

The application (A1) which gives a call to the function is called web service client; the application (A2) which gives services to the request is called the web service endpoint.
 
Note: In general words, “web service endpoint” is a service which published outside for user to access; where “web service client” is the party who access the published service.

This is a basic level over view of web services. In the next tutorial we will learn about some terminology used in web service.
 

Read More