Table of contents
1.
Introduction
2.
What is a Java web service?
3.
Types of Web Services
4.
Java Web Services API
5.
Hello World JAX-WS Application
6.
Hello World JAX-RS Application
7.
Examples of Java Web Services
8.
Implementation of Java Web Services
9.
Frequently Asked Questions
9.1.
What is a Web Service?
9.2.
What is an example of a web service?
9.3.
What are the advantages of Web Services?
9.4.
What are some advantages of SOAP Web Services?
10.
Conclusion
Last Updated: Sep 19, 2025

Web Services in Java – SOAP, REST & Examples

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Services that we can access over the network are called web services. So how does it differ from a web application? They are also services that are accessed over the network. There are a few attributes that state this difference.

  • Web applications are meant for users to be accessed in a human-readable browser, whereas web services are used for applications to access data in the format of JSON, XML, etc.
     
  • Web applications use HTTP/HTTPS protocol, whereas traditional web services use SOAP protocol. Recently REST is gaining popularity, an architecture style that runs on HTTP/HTTPS protocol.
     
  • Web applications are not meant for reusability, which is one of web services' benefits. Different kinds of applications can use a single web service.
     
  • Web applications can access web services to access some data or to perform some tasks, but web services cannot access web applications to fetch some data.
     
  • Web applications are capable of maintaining user sessions; web services are stateless.

I hope the above differences are good enough to clarify any confusion with web applications and services. Both are different concepts and are meant for different purposes.

Working of web services

Working of web services

What is a Java web service?

A Java web service is a software application built using the Java programming language that enables communication and data exchange between applications over a network, often the internet. Web services follow a standardized way to facilitate interoperability between different applications and systems, often using HTTP (Hypertext Transfer Protocol) as a communication protocol. They allow different systems, possibly written in different programming languages and running on different platforms, to interact seamlessly.

Types of Web Services

There are a few types of web services: SOAP, REST, XML-RPC, and UDDI.

  1. SOAP: It 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, its platform and language are independent. So our server can use JAVA, and the client can be on PHP, .NET, and vice versa.
     
  2. REST: It is an architectural style for developing web services. It is getting popular recently because it has a slight learning curve compared to SOAP. Resources are the core concepts of Restful web services, and their URIs uniquely identify them.
     
  3. UDDI: UDDI(Universal Description, Discovery, and Integration) is a platform-independent framework for describing, discovering, and integrating web services. It acts as a directory for businesses to list their web services and allows clients to locate services by browsing or searching. UDDI itself is not a communication protocol but a registry service.
     
  4. XML-RPC: XML-RPC(XML Remote Procedure Call) is a protocol that uses XML to encode its calls and HTTP as a transport mechanism. It allows programs to make remote procedure calls (RPCs) to other systems over the network. XML-RPC is simpler than SOAP and is a precursor to it.

Java Web Services API

Java provides its own API to create SOAP and REST web services.

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

These APIs are elements of standard JDK installation, so we do not need to add jars to work with them. Both of these APIs utilize annotations very heavily.

Hello World JAX-WS Application

Let’s create a very simple Hello World JAX-WS application.

File name:  TestService.java

package com.journaldev.jaxws.service;


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 TestService {


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

	public static void main(String[] args){
		Endpoint.publish("https://localhost:8888/testWS", new TestService());
	}
}

That’s it. Just run this application, and our Hello World JAX-WS SOAP web service is published. 

Hello World JAX-RS Application

Jersey is the reference implementation of the JAX-RS API; it is not included in the JDK by default, therefore we must add all necessary jar files. Create a straightforward Dynamic web project and then convert it to Maven in Eclipse for the best results. Here is the completed pom.xml file with the necessary dependencies.

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JAX-RS-HelloWorld</groupId>
<artifactId>JAX-RS-HelloWorld</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>

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="https://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JAX-RS-HelloWorld</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.journaldev.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>


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


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


@Path("/test")
public class TestService {


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

Just export it as a WAR file and then access it in the browser as shown in the below image.

Java Web Services tutorial, JAX-RS Example

Examples of Java Web Services

SOAP-based Web Service (JAX-WS):

  • SOAP-based services use JAX-WS (Java API for XML Web Services) for building web services that adhere to the SOAP protocol.
  • Example: An e-commerce platform might use a SOAP service to handle payment processing. The service can be called by both internal systems and external partners to validate and process transactions securely.

RESTful Web Service (JAX-RS):

  • RESTful services use JAX-RS (Java API for RESTful Web Services) to create services that follow REST principles, using HTTP methods for data operations.
  • Example: A weather API service that provides weather information to client applications (like mobile apps or websites). Each endpoint can represent a different resource (e.g., /weather/{city}), allowing clients to retrieve weather data for specific locations using GET requests.

Spring Boot REST Web Service:

  • Spring Boot simplifies the creation of RESTful web services with less configuration than traditional Java EE.
  • Example: A banking application where different microservices handle operations like account management, transaction processing, and customer support. Each service can be built as an independent Spring Boot application with REST endpoints for specific functionality.

XML-RPC Web Service:

  • XML-RPC services are less common in modern development but can be implemented using libraries like Apache XML-RPC.
  • Example: A legacy system integration where a simple XML-RPC service provides data from an old ERP (Enterprise Resource Planning) system to a newer application, enabling the two systems to communicate.

Microservices with Spring Cloud:

  • Using Spring Cloud with Spring Boot, you can create a suite of microservices with RESTful APIs that work together as a cohesive system.
  • Example: In an online retail platform, there can be separate microservices for user authentication, product catalog, shopping cart, and order processing, each exposed as a REST API.

Implementation of Java Web Services

1. SOAP-based Web Service Implementation (JAX-WS)

To create a SOAP-based service in Java using JAX-WS:

  1. Define the Service Interface: Annotate the interface with @WebService and each method with @WebMethod.
  2. Implement the Service Class: Implement the interface and add the business logic.
  3. Publish the Service: Use an Endpoint to publish the service on a specific URL.
  4. Generate the WSDL: When the service is published, you can access the WSDL at http://localhost:8080/PaymentService?wsdl, which clients use to consume the service.

2. RESTful Web Service Implementation (JAX-RS)

To create a RESTful service in Java using JAX-RS:

  1. Define the REST Resource Class: Annotate the class with @Path to specify the base URL, and use @GET, @POST, etc., to map HTTP methods.
  2. Configure the Application: Define an application class or specify configuration in a web.xml file.
  3. Deploy the Application: Deploy it on a compatible server like Apache Tomcat or JBoss. You can then access the RESTful service at http://localhost:8080/api/weather/{city}.

3. RESTful Web Service Implementation with Spring Boot

Spring Boot makes creating REST services simpler by handling most configurations for you:

  1. Create a Spring Boot Application: In Spring Initializr, add dependencies for Spring Web.
  2. Define a REST Controller: Use @RestController and map endpoints with @GetMapping, @PostMapping, etc.
  3. Run the Application: Spring Boot provides an embedded server, so just run the application, and the service will be available at http://localhost:8080/api/weather/{city}.

4. XML-RPC Web Service Implementation

  1. Set up XML-RPC Library: Use a library like Apache XML-RPC to handle XML encoding and decoding.
  2. Define the Service Class: Write a service method in a class that handles requests.
  3. Publish the Service: Register and start the service.

5. Microservices Implementation with Spring Cloud

With Spring Cloud, you can create microservices with added features like service discovery (Eureka), API gateways (Zuul), and more. Here’s an overview:

  1. Create a Spring Boot microservice with REST endpoints, like a User Service.
  2. Add Spring Cloud Dependencies for service discovery (e.g., Eureka), so services can register and discover each other.
  3. Build additional microservices (e.g., Order Service, Product Service), each with its own functionality and RESTful APIs.
  4. Use an API Gateway like Zuul to provide a unified entry point for all microservices, making routing and load balancing easier.

Frequently Asked Questions

What is a Web Service?

Client applications can access web services over the network because web services operate on a client-server basis. Web services offer endpoint URLs and expose methods used by client software created in Java, Shell script, or any other programming language. Unlike web apps, which keep user sessions, web services are stateless.

What is an example of a web service?

An example of a web service is the Google Maps API. This service provides location data, map rendering, and geographic information that other applications can integrate into their own systems. Google Maps API enables developers to access Google’s vast geographic data and use it within websites, mobile apps, and other platforms.

What are the advantages of Web Services?

  1. Interoperability: Web services can be created in any programming language because they can be accessed via a network, operate on the HTTP/SOAP protocol, and use XML/JSON to transfer data. We can use Java programming to create web services, while we can use PHP to create clients, and vice versa.
  2. Reusability: Multiple client applications can use a single web service concurrently.
  3. Loose Coupling: Our program has achieved loose coupling because the client code for Web services is entirely independent of the server code.

What are some advantages of SOAP Web Services?

  1. SOAP web services have all the advantages that web services have; some of the additional advantages are:
  2. The WSDL document provides contract and technical details of the web services for client applications without exposing the underlying implementation technologies.
  3. SOAP utilizes XML data for payload and contracts, so any technology can easily read it.

Conclusion

In this article, we have discussed Web Services. We have also discussed its types, specifically Java Web services. We also saw how to use these services in JAX- WS and JAX- RS applications along with its code.

Recommended Readings:

Live masterclass