Java 11 SOAP client example

Quentina 36 Published: 12/07/2024

Java 11 SOAP client example

I'll respond in English this time.

Java 11 provides a built-in module for creating SOAP (Simple Object Access Protocol) clients, making it easier to integrate with web services that use SOAP. Here's an example of how you can create a SOAP client using Java 11:

Step 1: Create a new Maven project

Create a new Maven project in your favorite IDE (Integrated Development Environment). If you don't have one, you can use Eclipse or IntelliJ IDEA.

Step 2: Add the necessary dependencies

In your pom.xml file, add the following dependencies:


javax.xml.soap

soap-api

1.4.7

org.glassfish.jaxb

jaxb-core

2.3.0.20180812

com.sun.xml.ws

jaxws-rt

2.3.1

These dependencies include the Java API for SOAP (JAX-SOAP), JAXB (Java Architecture for XML Binding), and JAX-WS (Java API for XML Web Services).

Step 3: Create a SOAP client class

Create a new Java class, e.g., SoapClient.java, with the following code:

import javax.xml.soap.SOAPEnvelope;

import javax.xml.soap.SOAPMessage;

import javax.xml.ws.Dispatch;

import javax.xml.ws.Service;

import javax.xml.ws.WebServiceException;

import java.io.IOException;

public class SoapClient {

public static void main(String[] args) throws IOException, WebServiceException {

// Create a service instance

Service service = Service.create(new URL("http://localhost:8080/soap-service"), "SOAP SERVICE");

// Get the port (endpoint)

Dispatch dispatch = service.getPort(Dispatch.class);

// Create a SOAP message

SOAPMessage message = MessageFactory.newInstance().createMessage();

// Set the SOAP action

message.setProperty(SOAPMessage.PROPERTY_SOAP_ACTION, "urn:soap-action");

// Create a SOAP envelope

SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

// Add some SOAP elements to the envelope

envelope.addNamespaceDeclaration("ns", "http://example.com");

SOAPElement root = envelope.getRootElement();

SOAPElement child = root.addContent(new SOAPElement("", "child-element"));

child.setTextContent("Hello, World!");

// Send the SOAP message to the server

dispatch.send(message);

// Get the response from the server

SOAPMessage response = (SOAPMessage) dispatch.receive();

// Process the response

System.out.println(response.getSOAPBody().getText());

}

}

This code creates a SOAP client that connects to a web service at http://localhost:8080/soap-service, sends a SOAP message, and processes the response.

Step 4: Run the SoapClient class

Compile your project and run the SoapClient class. This will create a SOAP client that can connect to the web service and send/receive SOAP messages.

In this example, we've created a simple SOAP client using Java 11's built-in support for SOAP. You can modify the code to suit your specific use case and integrate with your own web services.

Remember to replace http://localhost:8080/soap-service with the actual URL of your web service.

Java soap client example github

I'd be happy to help you with that!

The Java Soap Client Example GitHub repository provides a simple demonstration of how to use the Apache CXF library for creating a SOAP-based web service client.

To get started, let's first clone the repository from GitHub:

git clone https://github.com/apache/cxf.git

Once you have cloned the repository, navigate into the cxf directory and then create a new directory to hold your project. Let's call it soap-client-example.

Now, let's take a look at the SoapClientExample.java file located in the src/main/java directory of the soap-client-example. This file contains the Java code for creating a SOAP-based web service client using Apache CXF.

Here's the code:

package com.example.soapclient;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import org.apache.cxf.service.invoker.Invoker;

import org.apache.cxf.service.invoker.SimpleMethodInvoker;

public class SoapClientExample {

public static void main(String[] args) throws Exception {

// Create a JAX-WS client using Apache CXF

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setServiceClass(SayHello.class);

factory.setAddress("http://localhost:8080/soap");

SayHello proxy = (SayHello)factory.create();

// Call the sayHello method using the client

String response = proxy.sayHello("John");

System.out.println("Response: " + response);

}

}

Let's break down what this code does:

It creates a JaxWsProxyFactoryBean object, which is used to create a JAX-WS client using Apache CXF. The setServiceClass(SayHello.class) method specifies the web service class that we want to use (in this case, the SayHello class). The setAddress("http://localhost:8080/soap") method sets the URL of the web service endpoint. Then it creates an instance of the SayHello proxy using the create() method. Finally, it calls the sayHello() method on the proxy instance and prints out the response.

Now that you have a basic understanding of how to use Apache CXF for creating SOAP-based clients in Java, let's run our project!