This tutorial will show you how we can publish SOAP based webservice using Apache cxf, Spring and Maven. For this tutorial we will create one maven web project in Eclipse. Please read also https://www.jeejava.com/consume-soap-webservice-using-apache-cxf-spring-maven/

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.
Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8(if you use jdk 1.7 then use Tomcat 7)
Tomcat 8
Have maven installed and configured
Apache cxf and Spring dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

First we will create service project

Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-webapp

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : cxf-spring

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>cxf-spring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>cxf-spring Maven Webapp</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Apache cxf dependencies -->
        <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>
        <!-- servlet & jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- maven compiler plugin definition -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <!-- apache cxf plugin for generating wsdl -->
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-java2ws-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>process-classes</id>
                        <phase>process-classes</phase>
                        <configuration>
                            <!-- service endpoint interface or class -->
                            <className>com.roytuts.service.ProductService</className>
                            <!-- location where wsdl file will be generated -->
                            <outputFile>${project.basedir}/src/main/resources/wsdl/ProductService.wsdl</outputFile>
                            <genWsdl>true</genWsdl>
                            <verbose>true</verbose>
                            <!-- mention service address otherwise it will generate default URL -->
                            <!-- pattern : http://<host>:<port>/<project artifactId will be used as context-path here or you may have different context path>/<service end point> -->
                            <address>http://localhost:8080/${project.artifactId}/services/productService</address>
                        </configuration>
                        <goals>
                            <goal>java2ws</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create service endpoint interface in package com.roytuts.service

package com.roytuts.service;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.roytuts.model.Product;

@WebService
public interface ProductService {

    @WebMethod
    public Product getProduct(@WebParam(name = "productId") int productId);

    @WebMethod
    public List<Product> getAllProducts();

}

Step 5. Create service endpoint implementation class

package com.roytuts.service.impl;

import java.util.List;

import javax.jws.WebService;

import com.roytuts.mock.ProductServiceMockDaoImpl;
import com.roytuts.model.Product;
import com.roytuts.service.ProductService;

//mention endpoint interface and serviceName
@WebService(endpointInterface = "com.roytuts.service.ProductService", serviceName = "productService")
public class ProductServiceImpl implements ProductService {

    private ProductServiceMockDaoImpl productServiceMockImpl;

    public void setProductServiceMockImpl(ProductServiceMockDaoImpl productServiceMockImpl) {
        this.productServiceMockImpl = productServiceMockImpl;
    }

    @Override
    public Product getProduct(int productId) {
        return productServiceMockImpl.getProduct(productId);
    }

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

}

Step 6. Create model class Product

package com.roytuts.model;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Product")
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;

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

    public Product() {
    }

    public Product(int productId, String productName, String productCatg) {
        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;
    }

}

Step 7. Create mock data class because we are not using database to store data

package com.roytuts.mock;

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

import com.roytuts.model.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) {
        for (Product product : productList) {
            if (product.getProductId() == id) {
                return product;
            }
        }
        return null;
    }

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

}

Step 8. Create Spring XML configuration file called spring-beans.xml and put it under src/main/resources folder

<?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" />

    <bean id="mockDataLayer" class="com.roytuts.mock.ProductServiceMockDaoImpl" />

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

    <jaxws:endpoint id="productService" implementor="#productServiceImpl"
        address="/productService" />

</beans>

Step 9. Modify web.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Product Service Web Service</display-name>

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

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

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

Step 10. Now run “mvn clean compile install” command from command prompt on project directory “cxf-spring”. You will see that wsdl file is generated into the location “src/main/resources/wsdl/ProductService.wsdl”

Step 11. Now deploy the project into Tomcat 8 server from Eclipse. You can also access the wsdl file from the location “http://localhost:8080/cxf-spring/services/productService?wsdl”

The generated ProductService.wsdl looks like below under src/main/resources/wsdl folder

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="ProductServiceService" targetNamespace="http://service.roytuts.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.roytuts.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <wsdl:types>
<xs:schema xmlns:tns="http://service.roytuts.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://service.roytuts.com/" version="1.0">

  <xs:element name="Product" type="tns:product"/>

  <xs:element name="getAllProducts" type="tns:getAllProducts"/>

  <xs:element name="getAllProductsResponse" type="tns:getAllProductsResponse"/>

  <xs:element name="getProduct" type="tns:getProduct"/>

  <xs:element name="getProductResponse" type="tns:getProductResponse"/>

  <xs:complexType name="getProduct">
    <xs:sequence>
      <xs:element name="productId" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="getProductResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="tns:product"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="product">
    <xs:sequence>
      <xs:element minOccurs="0" name="productCatg" type="xs:string"/>
      <xs:element name="productId" type="xs:int"/>
      <xs:element minOccurs="0" name="productName" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="getAllProducts">
    <xs:sequence/>
  </xs:complexType>

  <xs:complexType name="getAllProductsResponse">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:product"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="getAllProductsResponse">
    <wsdl:part name="parameters" element="tns:getAllProductsResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getProduct">
    <wsdl:part name="parameters" element="tns:getProduct">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getProductResponse">
    <wsdl:part name="parameters" element="tns:getProductResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getAllProducts">
    <wsdl:part name="parameters" element="tns:getAllProducts">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="ProductService">
    <wsdl:operation name="getProduct">
      <wsdl:input name="getProduct" message="tns:getProduct">
    </wsdl:input>
      <wsdl:output name="getProductResponse" message="tns:getProductResponse">
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getAllProducts">
      <wsdl:input name="getAllProducts" message="tns:getAllProducts">
    </wsdl:input>
      <wsdl:output name="getAllProductsResponse" message="tns:getAllProductsResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ProductServiceServiceSoapBinding" type="tns:ProductService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getProduct">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getProduct">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getProductResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getAllProducts">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getAllProducts">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getAllProductsResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ProductServiceService">
    <wsdl:port name="ProductServicePort" binding="tns:ProductServiceServiceSoapBinding">
      <soap:address location="http://localhost:8080/cxf-spring/services/productService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

In next tutorial (https://www.jeejava.com/consume-soap-webservice-using-apache-cxf-spring-maven/) we will see how to consume this service.

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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