In this article I will explain how to expose SOAP web service using Apache CXF and Spring framework.

If you are using maven, add this to your maven pom file to download the required dependencies.

[code language=”xml”]

<properties>
<cxf.version>3.0.4</cxf.version>
<spring.version>4.0.5.RELEASE</spring.version>
</properties>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
[/code]

Let’s start with implementing the service layer. Here I am going to use develop a product service implementation. Please see the code snippets below.

Product Service Interface

[code language=”java”]
package com.questforapps.webservice.service;

import java.util.List;

import com.questforapps.webservice.beans.Product;

public interface ProductService {

public Product getProduct(int id);
public List<Product> getAllProducts();

}
[/code]

Product Service Implementation

[code language=”java”]
package com.questforapps.webservice.service;

import java.util.List;

import com.questforapps.webservice.beans.Product;
import com.questforapps.webservice.dao.ProductServiceMockDaoImpl;

public class ProductServiceImpl implements ProductService {

private ProductServiceMockDaoImpl productServiceMockImpl;

public void setProductServiceMockImpl(ProductServiceMockDaoImpl productServiceMockImpl) {
this.productServiceMockImpl = productServiceMockImpl;
}
public Product getProduct(int productId) {

return productServiceMockImpl.getProduct(productId);
}

public List<Product> getAllProducts() {
return productServiceMockImpl.getAllProducts();
}

}
[/code]

Mock Data Layer

[code language=”java”]
package com.questforapps.webservice.dao;

import java.util.ArrayList;
import java.util.List;

import com.questforapps.webservice.beans.Product;

public class ProductServiceMockDaoImpl {

List<Product> productList = new ArrayList<Product>();

public ProductServiceMockDaoImpl()
{

Product p1 = new Product(101,"Laptop", "Electronics");
Product p2 = new Product(102,"Bannana", "Fruits");
Product p3 = new Product(103,"Pencil", "Stationary");

productList.add(p1);
productList.add(p2);
productList.add(p3);

}

public Product getProduct(int id) {
return productList.get(id);
}

public List<Product> getAllProducts() {
return productList;
}

}
[/code]

Product object class

[code language=”java”]
package com.questforapps.webservice.beans;

public class Product {

private int productId;
private String productName;
private String productCatg;

public Product()
{

}

public Product(int productId, String productName, String productCatg) {
super();
this.productId = productId;
this.productName = productName;
this.productCatg = productCatg;
}

public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductCatg() {
return productCatg;
}
public void setProductCatg(String productCatg) {
this.productCatg = productCatg;
}

@Override
public String toString() {
return "Product [productId=" + productId + ", productName="
+ productName + ", productCatg=" + productCatg + "]";
}

}

[/code]

To expose the service as a soap web service we are using spring beans file with <jaxws:endpoint>.  This endpoint has got many different properties, but for ease we are using only two fields.

implementor – provide the service implementation class( In our case –  ProductServiceImpl).
address – provide the address to which the service needs to be exposed.

Spring bean file with CXF service endpoint

[code language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="mockDataLayer" class="com.questforapps.webservice.dao.ProductServiceMockDaoImpl"/>

<bean id="productServiceImpl" class="com.questforapps.webservice.service.ProductServiceImpl">
<property name="productServiceMockImpl">
<ref bean="mockDataLayer"/>
</property>
</bean>

<jaxws:endpoint
id="productEndpoint"
implementor="com.questforapps.webservice.service.ProductServiceImpl"
address="/products" />

</beans>
[/code]

Once this is done we need to add the CXF servlet configuration to web.xml file.  Please see the code snippet below to the web.xml file.

Web.xml

[code language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Product Service Web Service</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:beans.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<description>CXF Endpoint</description>
<servlet-name>webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

</web-app>

[/code]

Hope this article helped you to expose a SOAP web service using Apache CXF and Spring. Let me know if you have any questions or comments.

One Response

Leave a Reply

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