Consuming SOAP Service With Apache CXF and Spring

Volkan Güngör
turkcell
Published in
3 min readJan 4, 2022

SOAP web services are not popular anymore but if you are working with old applications, you probably still have to deal with SOAP web services. I am going to give you an example of how to consume a SOAP service with CXF, how to make a configuration for it, and how to log requests and responses to it.

A Simple Web Service

Before I can consume a web service, I need a simple web service to work with. For the project, I am going to use Spring Boot version 2.5.0 and Java 11. You can use another version if you like; it does not matter as long as the versions are not too old. You can easily create a Spring Boot project with a Spring Initializer. If you use maven, pom.xml should be like this. If you use Java 8, you do not need to have a “JAXB Runtime” dependency.

First of all, I will create an interface for my simple web service like below. As you see, it will get a single string parameter and returns a string response.

And, now, I need an implementation of this interface. It will just add “Hello” in front of the input parameter and return it.

This is not the topic of this article, so that’s why I am not going into detail with CXF configuration. You just have to configure CXF like below.

Now, if I start my Spring Boot application, I can reach WSDL of my simple web service like this: http://localhost:8080/ws/helloWorldWS?wsdl

I will just write a test class to test my simple web service as below:

As I run it, I see the below output. My web service works, now let’s get to the point.

Web Service Client Consumer Configuration

If you like, you can start a new project like the previous one to consume the HelloWorld web service. Before consuming a web service, I need a client jar file. I am going to use wsimport utility to create a client jar file, with a command like this;

wsimport -clientjar helloWorldWSClient.jar “http://localhost:8080/ws/helloWorldWS?wsdl"

Client jar is created and I need to add it to the project as a dependency. After that, I can create a CXF configuration for it like below:

I created a bean name as helloWorldWSClient, as you see. HelloWorldWS.class is the service class included in the client jar file. WSDL location is the path of the WSDL file in the jar file. serviceName consists of namespace and port name. You can put some configuration parameters into the properties map (like connection and socket timeout values). And the last thing to do is to set the endpoint of the service.

The LoggingFeature is required to log outgoing requests and incoming responses. If you want to log them you just have to put the below line into the application.properties file:

logging.level.org.apache.cxf.services = INFO

This will cause all CXF clients to log requests and responses. If you want to log only a specific service, then you need to have log configs like below.

logging.level.org.apache.cxf.services.HelloWorldWS.REQ_OUT = INFO

logging.level.org.apache.cxf.services.HelloWorldWS.RESP_IN = INFO

As it is obvious, you can use the service class name to log a specific service.

Consumer Web Service

Once the configuration is done, I am ready to call and consume our soap service. First of all, I am going to create a consumer service like below:

Now, I need to test this consumer service. I am going to write a simple test class with a simple assertion.

After running this test, let’s look at the console output.

That’s all.

--

--