What is a Web Service

A Web Service is can be defined by following ways:

  • is a client server application or application component for communication.
  • method of communication between two devices over network.
  • is a software system for interoperable machine to machine communication.
  • is a collection of standards or protocols for exchanging information between two devices or application.

Let’s understand it by the figure given below:

As you can see in the figure, java, .net or PHP applications can communicate with other applications through web service over the network. For example, java application can interact with Java, .Net and PHP applications. So web service is a language independent way of communication.

Types of Web Services

There are two types of web services.

  1. SOAP: SOAP stands for Simple Object Access Protocol. SOAP is an XML based industry standard protocol for designing and developing web services. Since it’s XML based, it’s platform and language independent. So our server can be based on JAVA and client can be on .NET, PHP etc. and vice versa.
  2. REST: REST is an architectural style for developing web services. It’s getting popularity recently because it has small learning curve when compared to SOAP. Resources are core concepts of Restful web services and they are uniquely identified by their URIs.

Java Web Services

Java provides it’s own API to create both SOAP as well as REST web services.

  1. JAX-WS: JAX-WS stands for Java API for XML Web Services. JAX-WS is XML based Java API to build web services server and client application.
  2. JAX-RS: Java API for RESTful Web Services (JAX-RS) is the Java API for creating REST web services. JAX-RS uses annotations to simplify the development and deployment of web services.

Both of these APIs are part of standard JDK installation, so we don’t need to add any jars to work with them. Both of these APIs use annotations very heavily.

Hello World JAX-RS Application

Jersey RESTful Web Services framework is an open source framework for developing RESTful Web Services in Java. It provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.It’s not part of standard JDK and we have to include all the required jars. Best way is to use Maven build, so create a simple Dynamic web project and then convert it to Maven in Eclipse.

Here is the final pom.xml file having required dependencies.

Now add Jersey servlet to our deployment descriptor web.xml as front controller.

Above two steps are required for initial setup, below is our Hello World JAX-RS service class.

Output

In this example, web request from “projectURL/rest/hello/” will match to “Hello“, via @Path(“/hello”). And the “{any values}” from “projectURL/rest/hello/{any values}” will match to parameter annotated with @PathParam.

URL : http://localhost:8080/WebServices_JAX_RS/rest/hello/CodeNuclear

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

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