Introduction

SOAP (Simple Object Access Protocol) is still widely used in enterprise applications for secure, structured communication. While RESTful services are more prevalent, SOAP remains essential in legacy systems. Quarkus, a Kubernetes-native Java framework, provides efficient ways to consume SOAP services while leveraging its fast startup and low memory footprint.

In this guide, we’ll explore how to consume a SOAP web service in Quarkus using JAX-WS and Apache CXF.

Prerequisites

Before starting, ensure you have the following:

  • Java 11 or later
  • Quarkus 2.x or later
  • Maven or Gradle
  • A WSDL file or an available SOAP web service

Setting Up the Quarkus Project

We will create a Quarkus project to consume a SOAP service using Apache CXF and JAX-WS.

Step 1: Create a Quarkus Project

Generate a Quarkus project using the Quarkus CLI or through the Quarkus project generator:

mvn io.quarkus.platform:quarkus-maven-plugin:2.0.0.Final:create \
    -DprojectGroupId=com.example \
    -DprojectArtifactId=soap-client \
    -DclassName="com.example.soapclient.ServiceClient" \
    -Dpath="/soapclient"

Step 2: Add Dependencies

Add the required dependencies in pom.xml:

<dependencies>
    <!-- Quarkus CXF for SOAP services -->
    <dependency>
        <groupId>io.quarkiverse.cxf</groupId>
        <artifactId>quarkus-cxf</artifactId>
    </dependency>
    
    <!-- JAX-WS for SOAP communication -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
    </dependency>
</dependencies>

Step 3: Generate Java Classes from WSDL

Use wsimport to generate Java classes from the WSDL file:

wsimport -keep -p com.example.client -d ./src/main/java http://example.com/service?wsdl

This generates the required classes in the com.example.client package.

Step 4: Configure the SOAP Client

Create a Quarkus SOAP client using Apache CXF:

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.example.client.MySoapService;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class SoapServiceClient {
    private static final String WSDL_URL = "http://example.com/service?wsdl";

    public MySoapService getService() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(MySoapService.class);
        factory.setAddress(WSDL_URL);
        return (MySoapService) factory.create();
    }
}

Step 5: Define a REST Endpoint for Testing

Create a REST controller to invoke the SOAP service:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.inject.Inject;

@Path("/invokeSoap")
public class SoapController {
    
    @Inject
    SoapServiceClient soapServiceClient;
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String invokeSoapService() {
        return soapServiceClient.getService().callSoapMethod();
    }
}

Testing the SOAP Client

Start the Quarkus application and call the REST endpoint to invoke the SOAP service:

GET http://localhost:8080/invokeSoap

If everything is configured correctly, the response should contain the SOAP service result.

External Resources

FAQs

  1. What is SOAP in Java?
    SOAP is a protocol that allows structured XML-based message exchange over HTTP, used in web services.
  2. Why use SOAP instead of REST?
    SOAP offers advanced security, transaction management, and is commonly used in enterprise systems.
  3. Can I consume a SOAP service in Quarkus without WSDL?
    Yes, but using a WSDL makes integration easier with auto-generated classes.
  4. What is JAX-WS, and why is it used?
    JAX-WS (Java API for XML Web Services) is a Java standard for building and consuming SOAP-based services.
  5. How does Quarkus handle SOAP requests?
    Quarkus leverages Apache CXF to handle SOAP requests efficiently.
  6. What is the difference between JAX-WS and JAX-RS?
    JAX-WS is used for SOAP-based web services, while JAX-RS is for RESTful web services.
  7. How do I debug SOAP responses in Quarkus?
    Enable logging with quarkus.log.level=DEBUG in application.properties.
  8. Can I use Quarkus with Spring Boot for SOAP services?
    Yes, but Quarkus is designed to be lightweight and cloud-native compared to Spring Boot.
  9. Is SOAP still relevant today?
    Yes, many enterprises rely on SOAP for secure and transactional web services.
  10. How do I secure a SOAP service in Quarkus?
    Use WS-Security for authentication or integrate OAuth for token-based security.

Conclusion

Consuming SOAP services in Quarkus is efficient with Apache CXF and JAX-WS. This guide provides a step-by-step approach to integrating SOAP-based APIs in Quarkus applications, ensuring maintainability and performance optimization. By following best practices, you can seamlessly integrate SOAP services into your Quarkus projects.