use of javax xml ws Endpoint class

use of javax xml ws Endpoint class

The javax.xml.ws.Endpoint class in JAX-WS API is used to publish and manage web service endpoints. It provides methods to create and publish a web service on a specified address, making it accessible to clients. The Endpoint class is a crucial part of the JAX-WS API, simplifying the deployment process of SOAP web services.

use of javax xml ws Endpoint class

Key Features

  • 1.  Publishing Web Services : Easily publish a web service on a specified URL.
  • 2.  Managing Endpoint Lifecycle : Start, stop, and check the status of the endpoint.
  • 3.  Customization : Customize the endpoint’s behavior through configuration settings and properties.

Explanation in Java Example

Let’s create a simple SOAP web service using the Endpoint class to demonstrate its usage.

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 using JAX-WS annotations.

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

@WebService
public class CalculatorService {

    @WebMethod
    public int add(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        return a + b;
    }

    @WebMethod
    public int subtract(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        return a - b;
    }

    public static void main(String[] args) {
        // Publish the web service at the specified URL
        Endpoint endpoint = Endpoint.publish("http://localhost:8080/ws/calculator", new CalculatorService());
        
        // Check if the endpoint is published
        if (endpoint.isPublished()) {
            System.out.println("CalculatorService is published successfully.");
        } else {
            System.out.println("Failed to publish CalculatorService.");
        }
    }
}

Explanation

  • 1.  Endpoint Publishing :
    • The Endpoint.publish method is used to publish the web service at the specified URL (http://localhost:8080/ws/calculator).
    • This makes the web service accessible to clients.
  • 2.  Endpoint Management :
    • The Endpoint class provides methods to manage the lifecycle of the web service endpoint.
    • In the example, endpoint.isPublished() is used to check if the endpoint is successfully published.
  • 3.  Web 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 @WebParam annotation customizes the names of the parameters in the SOAP message.