JAX WS API

JAX WS API

JAX WS (Java API for XML Web Services)  is a part of the Java EE platform that provides a standard way to create and consume web services. It allows developers to build web services and web service clients that communicate using XML messages. JAX-WS uses annotations to simplify the development of web services and clients.

JAX WS API

Key Features of JAX-WS

  • 1.  Annotation-Driven : Simplifies web service development by using annotations to define web service endpoints.
  • 2.  SOAP-Based : Supports SOAP (Simple Object Access Protocol) for message communication.
  • 3.  Interoperability : Ensures interoperability between Java applications and web services built using other technologies.
  • 4.  WSDL Support : Automatically generates and consumes WSDL (Web Services Description Language) files.
  • 5.  Integration with JAXB : Uses JAXB (Java Architecture for XML Binding) for XML data binding, making it easier to work with XML data.

Explanation in Java Example

Let’s create a simple SOAP web service using JAX-WS that performs basic arithmetic operations.

1. Maven Dependencies (pom.xml)
xml
<dependencies>
    <dependency>
        <groupId>javax.xml.ws</groupId>
        <artifactId>jaxws-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

2.  Service Endpoint Implementation (CalculatorService.java) :

Create a class to implement the web service. Use the @WebService annotation to define the web service and @WebMethod to define the web methods.

Syntax
java
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class CalculatorService {

    @WebMethod
    public int add(int a, int b) {
        return a + b;
    }

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

    public static void main(String[] args) {
        // Publish the web service at the specified URL
        Endpoint.publish("http://localhost:8080/ws/calculator", new CalculatorService());
    }
}

3.  Running the Service :

Run the CalculatorService class to publish the web service. The service will be available at http://localhost:8080/ws/calculator.

4.  WSDL Generation :

Access the WSDL file for the service by navigating to http://localhost:8080/ws/calculator?wsdl in your web browser. The WSDL file defines the contract for the web service, including the available operations and their parameters.

5.  Client Implementation (CalculatorClient.java) :

Create a client to consume the web service. Use the wsimport tool to generate the client artifacts from the WSDL file.

Run the following command to generate the client artifacts:

sh

wsimport -keep -p com.example.client http://localhost:8080/ws/calculator?wsdl

This command generates Java classes in the com.example.client package based on the WSDL file.

Create the client class to call the web service methods.

Syntax
java
import com.example.client.CalculatorService;
import com.example.client.CalculatorService_Service;

public class CalculatorClient {

    public static void main(String[] args) {
        // Create a service instance
        CalculatorService_Service service = new CalculatorService_Service();
        CalculatorService calculator = service.getCalculatorServicePort();

        // Call the web service methods
        int resultAdd = calculator.add(10, 5);
        int resultSubtract = calculator.subtract(10, 5);

        System.out.println("Addition Result: " + resultAdd);
        System.out.println("Subtraction Result: " + resultSubtract);
    }
}

Explanation

  • 1.  Service Implementation :
    • The CalculatorService class defines the web service with two methods: add and subtract. These methods are annotated with @WebMethod, indicating they are exposed as web service operations.
    • The Endpoint.publish method publishes the web service at the specified URL.
  • 2.  Client Implementation :
    • The CalculatorClient class uses the generated client artifacts to call the web service methods.
    • The CalculatorService_Service class is generated by the wsimport tool and provides a factory method to obtain a reference to the web service port.