Of course! This is a classic and foundational topic in enterprise Java development. Here's a comprehensive guide to creating and consuming a web service using WSDL in Java.

We'll cover:
- What is a Web Service? (A quick refresher)
- What is WSDL? (The "Contract")
- The Two Main Java Approaches:
- JAX-WS (Java API for XML Web Services): The modern, standard, and recommended way.
- JAX-RS (Java API for RESTful Web Services): For REST services (often use JSON, not WSDL).
- Step-by-Step Guide: Creating a JAX-WS Web Service (Server/Provider)
- Step-by-Step Guide: Consuming a JAX-WS Web Service (Client)
- Key Tools and Concepts
- Best Practices
What is a Web Service?
A web service is a method of communication between two electronic devices over a network. It's a software system designed to support interoperable machine-to-machine interaction over a network. In Java, this typically means a Java application exposing some functionality that can be called by applications written in other languages (like C#, Python, etc.).
What is WSDL?
WSDL (Web Services Description Language) is an XML-based language that describes a web service and how to access it. Think of it as the contract or the blueprint of your web service.
A WSDL file defines:

- What the service does: The operations it provides (e.g.,
getCustomer,placeOrder). - How to access it: The protocol (e.g., SOAP/HTTP).
- The data format: The structure of the messages (requests and responses) using XML Schema (XSD).
- Where to access it: The network address (URL) of the service.
The Two Main Java Approaches
A. JAX-WS (Java API for XML Web Services)
This is the standard for creating SOAP-based web services in Java. SOAP (Simple Object Access Protocol) is a protocol that uses XML for message formatting. It's robust, highly secure, and supports transactions, making it ideal for enterprise-level applications.
- Focus: SOAP, XML, WSDL.
- How it works: You write a standard Java interface and implementation class. JAX-WS tools then read your Java code and automatically generate the WSDL file. It can also do the reverse: read a WSDL file and generate the necessary Java client-side code (stubs/proxies).
- Annotation-Driven: You use simple annotations like
@WebService,@WebMethod, and@WebParamon your Java code.
B. JAX-RS (Java API for RESTful Web Services)
This is the standard for creating RESTful web services. REST (Representational State Transfer) is an architectural style that uses standard HTTP methods (GET, POST, PUT, DELETE). It typically uses JSON for data transfer, which is lighter and easier for humans to read than XML.
- Focus: REST, HTTP, JSON/XML.
- WSDL: RESTful services do not have a WSDL file. Their "contract" is implicit in the URL structure, HTTP methods, and media types (like JSON Schema).
- Annotation-Driven: You use annotations like
@Path,@GET,@POST,@Produces, and@Consumes.
For this guide, we will focus entirely on JAX-WS, as it's the technology directly related to WSDL.
Step-by-Step Guide: Creating a JAX-WS Web Service (Server)
Let's create a simple "Hello World" web service.

Prerequisites
- Java Development Kit (JDK) 8 or later.
- An IDE like IntelliJ IDEA or Eclipse.
- A web server like Apache Tomcat (to deploy the service).
- Maven for dependency management.
Step 1: Create a Maven Project
Create a new Maven project. In your pom.xml, add the JAX-WS API dependency. Note that you don't need a JAX-WS implementation like Metro or Apache CXF, as most modern Java application servers (like Tomcat) include one.
<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>com.example</groupId>
<artifactId>jax-ws-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JAX-WS API -->
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Plugin to create a WAR file for deployment -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
Step 2: Create the Service Endpoint Interface (SEI)
This is a Java interface that defines the methods your web service will expose. The @WebService annotation marks it as a web service interface.
src/main/java/com/example/HelloWorld.java
package com.example;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
@WebService
public interface HelloWorld {
/**
* A simple web method that returns a greeting.
* @param name The name to greet.
* @return A greeting string.
*/
@WebMethod
String sayHello(@WebParam(name = "name") String name);
/**
* A web method that adds two numbers.
* @param a The first number.
* @param b The second number.
* @return The sum of a and b.
*/
@WebMethod
int add(@WebParam(name = "a") int a, @WebParam(name = "b") int b);
}
Step 3: Create the Service Implementation
This class implements the SEI. The WebService annotation here links the implementation to the interface.
src/main/java/com/example/HelloWorldImpl.java
package com.example;
import jakarta.jws.WebService;
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
@Override
public int add(int a, int b) {
return a + b;
}
}
Step 4: Create the Publisher
This is a standalone Java class with a main method. It's responsible for publishing our service implementation on a specific URL and making it available.
src/main/java/com/example/HelloWorldPublisher.java
package com.example;
import jakarta.xml.ws.Endpoint;
public class HelloWorldPublisher {
public static void main(String[] args) {
// The URL where the service will be published
String url = "http://localhost:9999/ws/hello";
// Publish the implementation
Endpoint.publish(url, new HelloWorldImpl());
System.out.println("Service is published at: " + url);
System.out.println("To view the WSDL, go to: " + url + "?wsdl");
}
}
Step 5: Run and Test
- Run the
HelloWorldPublisherclass from your IDE. - Open a web browser and navigate to
http://localhost:9999/ws/hello?wsdl. - You should see the generated WSDL XML file. This is your service's contract!
You can also test it using a tool like SoapUI or by creating a simple Java client (see next section).
Step-by-Step Guide: Consuming a JAX-WS Web Service (Client)
Now, let's create a Java client that consumes the service we just published.
Step 1: Generate Client Stubs from WSDL
The easiest way to consume a web service is to let the JAX-WS tools generate the necessary Java classes (stubs/proxies) from the WSDL file. You can do this using the wsimport command-line tool.
Open your terminal or command prompt and run:
wsimport -keep -p com.example.client http://localhost:9999/ws/hello?wsdl
Explanation of options:
