Know what are the types of Java Web Services?

Last updated on Jun 17,2021 16.4K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.

Know what are the types of Java Web Services?

edureka.co

When you interact with any web page, it involves request and response through an HTML page. Similarly, web services also involve request and response, but in the form of XML or JSON. Java, being a suitable language for server-side communication, allows interoperability between different applications on different platforms. In this Java Web Services tutorial, I will give you a brief insight into different types of web services.

Below topics are covered in this article:

Let’s get started!

What is a Web Service?

Web service is an appropriate medium to propagate communication between the client and server applications on the World Wide Web. It is a software module which is designed to perform a certain set of tasks as follows:

For Example, Flipkart provides a web service that provides prices for products sold online via flipkart.com. The front end or presentation layer can be in .Net or Java but either programming language would have the ability to communicate with the web service.

With this, let’s understand the advantages of using Web Services.

Advantages of Web Services

Below pointers depicts the various advantages of Web Services.

  1. Exposing Business Functionality on the network – A web service is a managed code unit that provides some sort of functionality to client applications or end-users. This functionality can be invoked over the HTTP protocol which means that it can also be invoked over the internet. Web services can be used anywhere on the internet and provide the necessary functionality as required.

  2. Interoperability amongst applications – Web services allow various applications to communicate with each other and share data and services among themselves.

  3. A standardized Protocol which everybody understands Web services use standardized industry protocol for the communication. All four layers (Service Transport, XML Messaging, Service Description, and Service Discovery layers) uses well-defined protocols in the web services protocol stack.

  4. Reduction in cost of communication – Web services make use of SOAP over HTTP protocol, which lets you use your existing low-cost internet for implementing web services.

Now, let’s move further and understand the various types of web services that are used for communication on a day to day basis.

Types of Web Services

Generally, there are two types of web services as follows:

  1. Soap Web Services
  2. RESTful Web Services

Let’s get into the details of these web services.

1. Soap Web Services

Simple Object Access Protocol (SOAP) is a standard protocol specification for message exchange based on XML. Communication between the web service and the client happens using XML messages. A simple web service architecture has two component: viz. Client and Service provider.

In the above figure, you can notice how the client will communicate with the service provider. So in order to communicate, the client must know information like, the Location of web services server, Functions available, signature and return types of function and input-output formats. The service provider will create a standard XML file which will have all the above information. So, if this file is given to the client then it will be able to access web service. 

So that’s how it works. Now let’s move further and understand what is RESTful web services.

2. RESTful Web Services

Representational State Transfer (REST) is a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs.  It consists of two components REST server which provides access to the resources and a REST client which accesses and modify the REST resources. Take a look at the below figure for the same.

In this REST architecture style, client and server exchange the representations of the resources by using a standardized interface and protocol. REST is not a specific protocol, but when people talk about REST they usually mean REST over HTTP. The response from the server is considered as the representation of the resources. This representation can be generated from one or more number of resources. RESTful web services use HTTP protocol methods for the operations they perform. It includes methods like GET, POST, DELETE, etc.

Now that you know what are SOAP and RESTful web services, let’s move further and see how actually it works with the help of an example.

Java Web Services API

There are two main API’s defined by Java for developing web service applications.

1. JAX-WS: It is mainly for SOAP web services. There are two ways to write JAX-WS application code: by RPC style and Document style.

2. JAX-RS: It is mainly for RESTful web services. There is mainly 2 implementation currently in use for creating JAX-RS application: Jersey and RESTeasy.

Both of these Java APIs are part of standard JDK Installation, so you need not add any jars to work with them. Now, let’s get into the details of these APIs and understand how it works.

JAX-WS Example

Let’s create a simple Hello World JAX-WS application. Here, I will create a simple class file called Demo.java and write the program as shown below to display a simple message.

package com.Edureka;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;

@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class Demo {

@WebMethod
public String sayHello(String msg){
return "Hello "+msg;
}

public static void main(String[] args){
Endpoint.publish("http://localhost:8888/DemoWS", new Demo());
}
}

You can just run this application and the message of  JAX-WS SOAP web service is published. That’s how you need to code a simple JAX-WS application. 

Now let’s understand one more API of Java Web Services i.e. JAX-RS.

JAX-RS Example

Jersey is the reference implementation of JAX-RS API, it’s not part of standard JDK and you have to include all the required jars. The best way is to use Maven build, so create a simple Dynamic web project and then convert it to Maven in Eclipse.

In order to create a JAX-RS application, you need to follow the below-mentioned steps.

Step 1: Add the dependencies to pom.xml file as shown below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JAX-RS-HelloEdureka</groupId>
<artifactId>JAX-RS-HelloEdureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Step 2: Now, the next step is to add Jersey servlet to our deployment descriptor web.xml as a front controller.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JAX-RS-HelloEdureka</display-name>

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.Edureka.jaxrs.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Step 3: After all that, now let’s create a simple JAX-RS service class.

package com.Edureka;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/demo")
public class DemoWS {

@GET
@Path("/hello/{msg}")
public String sayHello(@PathParam(value="msg") String msg){
return "Hello "+msg;
}
}

After configuring the above file, you just have to export it as a WAR file and then access it in the browser. You will get the output as shown below.

Congratulations!

This brings us to the end of the article on Java Web Services. I hope I have thrown some light and added your knowledge on Web Services fundamentals.

Check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this Java Web Services tutorial and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES