Java SOAP Demo
SOAP (Simple Object Access Protocol) is a protocol that allows for exchanging structured information in web services using XML. In this article, we will explore how to develop a Java SOAP client to consume a SOAP web service.
Prerequisites
Before we begin, make sure you have the following:
- Java Development Kit (JDK) installed on your machine
- An integrated development environment (IDE) such as Eclipse or IntelliJ IDEA
- Basic knowledge of Java programming language
Setting up the SOAP Web Service
To demonstrate the Java SOAP client, we first need a SOAP web service to consume. For this example, let's create a simple SOAP web service that converts temperature from Celsius to Fahrenheit.
package com.example.soap;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class TemperatureConverter {
@WebMethod
public double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
}
In the above code, we define a web service class TemperatureConverter
with a single method celsiusToFahrenheit
that takes a temperature in Celsius as input and returns the corresponding temperature in Fahrenheit.
Generating the SOAP Client Code
To generate the SOAP client code, we can use tools like Apache CXF or JAX-WS. For this example, we will use Apache CXF. Add the following dependencies to your Maven pom.xml
file:
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
Now, let's generate the client code using the Apache CXF Maven plugin. Add the following configuration to your Maven pom.xml
file:
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.4.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/temperature-converter.wsdl</wsdl>
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-verbose</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Create a WSDL file temperature-converter.wsdl
in the src/main/wsdl
directory with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="
xmlns:tns="
targetNamespace="
<wsdl:types>
<xs:schema xmlns:xs=" targetNamespace="
<xs:element name="celsiusToFahrenheit" type="xs:double"/>
<xs:element name="celsiusToFahrenheitResponse" type="xs:double"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="celsiusToFahrenheitRequest">
<wsdl:part name="parameters" element="tns:celsiusToFahrenheit"/>
</wsdl:message>
<wsdl:message name="celsiusToFahrenheitResponse">
<wsdl:part name="parameters" element="tns:celsiusToFahrenheitResponse"/>
</wsdl:message>
<wsdl:portType name="TemperatureConverterPortType">
<wsdl:operation name="celsiusToFahrenheit">
<wsdl:input message="tns:celsiusToFahrenheitRequest"/>
<wsdl:output message="tns:celsiusToFahrenheitResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TemperatureConverterSoapBinding" type="tns:TemperatureConverterPortType">
<soap:binding style="document" transport="
<wsdl:operation name="celsiusToFahrenheit">
<soap:operation soapAction=""/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TemperatureConverterService">
<wsdl:port name="TemperatureConverterPort" binding="tns:TemperatureConverterSoapBinding">