Friday, February 28, 2014

Apache CXF Spring Integration Example

Apache CXF is a robust web service framework written in java that fully supports for the Spring based web service development. the purpose of this article is to share my experience on Apache CXF web service development on Spring framework. this example has been well tested with SoapUI.

the project structure will be as follows.




pom.xml


<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.chathurangaonline.apache.cxf.jaxws.spring.samples</groupId>
    <artifactId>apache-cxf-jaxws-spring-sample</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>apache-cxf-jaxws-spring-sample Maven Webapp</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</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>

    </dependencies>
    <build>
        <finalName>apache-cxf-jaxws-spring-sample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>



CalculatorService.java


package com.chathurangaonline.apache.cxf.jaxws.spring.samples;

import javax.jws.WebService;

/**
 * <p>
 *     SIB
 * </p>
 * @author Chathuranga Tennakoon / www.chathurangaonline.com
 */
@WebService
public interface CalculatorService {

    double multiply(double num1,double num2);
}



CalculatorServiceImpl.java


package com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl;

import com.chathurangaonline.apache.cxf.jaxws.spring.samples.CalculatorService;
import javax.jws.WebService;

/**
 * <p>
 *     SIB for {@link com.chathurangaonline.apache.cxf.jaxws.spring.samples.CalculatorService}
 * </p>
 * @author Chathuranga Tennakoon / www.chathurangaonline.com
 */
@WebService
public class CalculatorServiceImpl implements CalculatorService {

    @Override
    public double multiply(double num1, double num2) {
        return num1 * num2;
    }
}



web.xml


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

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

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

    <servlet>
        <servlet-name>AppCXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AppCXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>


applicationConxtext.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"/>

    <jaxws:endpoint id="calServiceEndpoint"
                    implementor="com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorServiceImpl"
                    address="/calcService"/>

</beans>



Once the web service is successfully deployed on web server(in my case it is tomcat), you can access the WSDL from following url.

http://localhost:8080/apache-cxf-jaxws-spring-sample/calcService?wsdl


The source code can be downloaded at:-
Get Source code From GitHub


Thanks
Chathuranga Tennakoon
www.chathurangaonline.com





No comments:

Post a Comment