How to build Soap Web Services with Apache CXF and Spring Boot

The popularity of Restful Services is rising that does not mean we have forgotten the Soap Web Services. In this article, we will see how to build Soap web services with Apache CXF and Spring Boot.

Softwares used

  • Spring Boot 1.5.10.RELEASE
  • cxf-spring-boot-starter-jaxws (3.1.12)
  • Java 8
  • Maven
  • Eclipse

Apache CXF is a popularly used services framework and its available as open source. Apache community has now added support for Spring Boot starter dependency so you don’t have to add each capability separately.

Maven Dependencies

This covers all the dependencies you need for creating your web service. But if you need to add any feature e.g. Request Logging, its available out of the box and you need to add that dependency separately.

Apache CXF Service Configuration

The first thing we have to do is configure CXFServlet and SpringBus. These two provides integration point with Spring.

With above configuration, all the service endpoints will be available at URL /services/*

Service Definition

We will see service definition using standard XML as well as using Java configuration.

XML Configuration

The above configuration will expose soap endpoint at given address. Also, notice that We have also mentioned logging feature to be enabled. This feature will make sure that soap request along with other request related parameters is logged. This data is logged at two points, one when a request is first received by framework and two, just before a response is sent out.

Let’s see corresponding java classes:

Greeting Service Interface

Greeting Service Implementation

Java Configuration

The Java configuration of an endpoint is pretty straightforward. Just define the bean as below:

This bean will expose a new endpoint at /InfoService.

InfoService

InfoService Implementation

The Model

As you noticed we are sending an object as response. The model for that is as:

Running the service

To make sure that Spring takes note of our XML web services definitions, we need to import it into the main application. The same you can see below:

Now go ahead and run your application. It will start at default port 8080. Once started hit the URL: http://localhost:8080/services/ You will see a screen like:

You can even load the WSDL by clicking on the links given in above page.

We can even test this with SoapUI tool

Adding Request Interceptors

Sometimes simply logging the incoming request is not enough. You might have to do some additional processing on the payload. For that Apache CXF has In and Out interceptors at different phases of a request. For illustrations, we will simply capture the incoming request.

Define In Interceptor

Define Out Interceptor

Now how do we attach these to our message? Well, these you can do at two places. one is at Bus level which will be applicable for all the endpoints managed by that Bus. The second option is to attach this interceptor to a particular endpoint.

That’s it!! Your custom interceptors are ready to take on any message that comes your way.

Conclusion

In this article, we have seen how easy it is to configure and run soap web service using Apache CXF and Spring Boot.

You can download complete source code from our GitHub repository.

Download Code

 

2 Comments
  1. Ola
    July 12, 2018 | Reply
    • Pavan
      July 16, 2018 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *