Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Creating and Consuming SOAP Web Services


SOAP (Simple Object Access Protocol) is a protocol used for exchanging structured information in the implementation of web services. It is an XML-based messaging protocol that allows applications to communicate over the network. In this article, we will guide you through the process of creating and consuming SOAP web services in advanced Java using the JAX-WS API.

1. What is SOAP and JAX-WS?

SOAP is a protocol for exchanging messages in a platform-agnostic and language-independent way, using XML to encode its HTTP-based communication. JAX-WS (Java API for XML Web Services) is a set of APIs that allows you to create SOAP web services and clients in Java. It supports both RPC (Remote Procedure Call) and document-style web services.

2. Setting Up the Development Environment

To create SOAP web services with JAX-WS, you need a Java development environment. If you're using Maven, you can include the necessary dependencies in your pom.xml file. Below is an example of the required dependencies for a JAX-WS-based project.

Example: Maven Dependencies

            
            <dependencies>
                <dependency>
                    <groupId>javax.xml.ws</groupId>
                    <artifactId>jaxws-api</artifactId>
                    <version>2.3.1</version>
                </dependency>
                <dependency>
                    <groupId>org.glassfish.jersey.core</groupId>
                    <artifactId>jersey-server</artifactId>
                    <version>2.31</version>
                </dependency>
            </dependencies>
            
        

3. Creating a SOAP Web Service

Once your environment is set up, you can begin creating a SOAP web service. You need to define a service class and annotate it with @WebService to mark it as a SOAP endpoint. Additionally, methods inside this class can be exposed as SOAP operations by annotating them with @WebMethod.

Example: Creating a Simple SOAP Service

            
            import javax.jws.WebMethod;
            import javax.jws.WebService;

            @WebService
            public class EmployeeService {

                @WebMethod
                public String getEmployeeDetails(int id) {
                    // Logic to fetch employee details from a database or in-memory
                    return "Employee ID: " + id + ", Name: John Doe, Position: Developer";
                }
            }
            
        

The EmployeeService class is marked with the @WebService annotation. The getEmployeeDetails method is annotated with @WebMethod to indicate that this method should be available as a web service operation.

4. Publishing the SOAP Web Service

Once the service class is ready, you need to publish it using the Endpoint class provided by JAX-WS. The Endpoint class is used to publish the service to a specified URL.

Example: Publishing the Service

            
            import javax.xml.ws.Endpoint;

            public class EmployeeServicePublisher {
                public static void main(String[] args) {
                    EmployeeService employeeService = new EmployeeService();
                    Endpoint.publish("http://localhost:8080/employeeService", employeeService);
                    System.out.println("Web service published at http://localhost:8080/employeeService");
                }
            }
            
        

In the EmployeeServicePublisher class, we create an instance of EmployeeService and publish it at http://localhost:8080/employeeService.

5. Generating the WSDL for the SOAP Service

After publishing the service, you can access its WSDL (Web Services Description Language) by appending ?wsdl to the service URL. The WSDL provides the definition of the web service and its methods, allowing clients to understand how to interact with the service.

Example: Accessing WSDL

            
            http://localhost:8080/employeeService?wsdl
            
        

When you access the above URL in a browser, the WSDL document will be generated, providing information about the available operations and message structures.

6. Consuming a SOAP Web Service

Once the SOAP service is published, you can create a client to consume the service. JAX-WS provides a Service class to create a proxy for the SOAP service. This proxy allows you to invoke the web service methods as local Java method calls.

Example: Creating a SOAP Client

            
            import javax.xml.ws.Service;
            import java.net.URL;
            import javax.xml.namespace.QName;

            public class EmployeeServiceClient {
                public static void main(String[] args) throws Exception {
                    // URL to the WSDL of the published service
                    URL url = new URL("http://localhost:8080/employeeService?wsdl");

                    // Create a QName object with the namespace and service name
                    QName qname = new QName("http://example.com/", "EmployeeService");

                    // Create the service object
                    Service service = Service.create(url, qname);

                    // Get the proxy to the web service
                    EmployeeService employeeService = service.getPort(EmployeeService.class);

                    // Call the web service method
                    String response = employeeService.getEmployeeDetails(123);
                    System.out.println("Response: " + response);
                }
            }
            
        

The EmployeeServiceClient class demonstrates how to consume the SOAP service. The client retrieves the WSDL, creates a service object, and then calls the getEmployeeDetails method of the SOAP service.

7. Testing the SOAP Web Service

After setting up the service and client, run both the EmployeeServicePublisher and EmployeeServiceClient classes. The service will be hosted at the given URL, and the client will make a SOAP request to fetch employee details.

8. Conclusion

In this article, we walked through the steps of creating and consuming SOAP web services using JAX-WS in advanced Java. We created a simple SOAP web service, published it, accessed the WSDL, and consumed the service using a client. By following these steps, you can create and integrate SOAP-based services into your Java applications for communication over the network.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java