Friday, February 28, 2014

Apache CXF First JAX-WS (SOAP Web Service) Example (CXFNonSpringServlet)

The most of the people think that the Apache CXF supports only to develop Spring based Web Services. this is NOT true. is supports to build both Spring based and Non spring based web services.

1. CXFServlet -  this will support for the Spring based web services.
2. CXFNonSpringServlet -  this will support for the non spring web services.


the purpose of this article is to demonstrate how to develop non spring based web service with Apache CXF. i have already developed and  tetsed the service with SoapUI. i am just sharing the source code for your reference.

the project structure looks as below.,


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.csf.jax.ws.samples</groupId>
    <artifactId>apache-cxf-jaxws-sample</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>apache-cxf-jaxws-sample Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <cxf.version>3.0.4</cxf.version>
    </properties>

    <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>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>apache-cxf-calculator-service</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!--compiles with java 7-->
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!--WAR plugin-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
            </plugin>
        </plugins>
    </build>
</project>



CalculatorService.java

the following is the Service Endpoint Interface(SEI).

package com.chathuranga.apache.cxf.jax.ws.samples;

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

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

    @WebMethod
    double addNumbers(double num1,double num2);
}
 



CalculatorServiceImpl.java

the following is the Service Implementation Bean (SIB)

package com.chathuranga.apache.cxf.jax.ws.samples.impl;

import com.chathuranga.apache.cxf.jax.ws.samples.CalculatorService;
import javax.jws.WebMethod;
import javax.jws.WebService;

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

    @WebMethod
    public double addNumbers(double num1, double num2) {
        return num1 + num2;
    }
}



AppCXFNonSpringServletImpl.java

the custom implementation for the CXFNonSpringServlet.

package com.chathuranga.apache.cxf.jax.ws.samples.impl;

import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import javax.servlet.ServletConfig;

/**
 * <p>
 *    Application Specific Custom Implementation for the {@link org.apache.cxf.transport.servlet.CXFNonSpringServlet}
 * </p>
 * @author Chathuranga Tennakoon / www.chathurangaonline.com
 */
public class AppCXFNonSpringServletImpl extends CXFNonSpringServlet{

    @Override
    public void loadBus(ServletConfig servletConfig){
        super.loadBus(servletConfig);
        ServerFactoryBean factory = new ServerFactoryBean();
        factory.setBus(bus);
        factory.setServiceClass(CalculatorServiceImpl.class);
        factory.setAddress("/calcService");
        factory.create();
    }
} 


web.xml

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         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_3_0.xsd">

    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>AppCXFNonSpringServlet</servlet-name>
        <servlet-class>com.chathuranga.apache.cxf.jax.ws.samples.impl.AppCXFNonSpringServletImpl</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AppCXFNonSpringServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>



Source code can be downloaded at :- https://github.com/chathurangat/apache-cxf-jax-ws-first-service


Chathuranga Tennakoon
www.chathurangaonline.com
chathuranga.t@gmail.com

1 comment: