Skip to main content

CXF Example –Web Service Using Spring and Maven

Apache CXF is an open source services framework. CXF helps you build Web Services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

  • Support for bottom up approach and top down approach.

Support for Standards

  • JAX-WS, JSR-181, SAAJ, JAX-RS
  • SOAP 1.1, 1.2, WS-I BasicProfile, WS-Security, WS-Addressing, WS-RM, WS-Policy
  • WSDL 1.1
  • MTOM

Building Web Services – Example 1

Develop a simple Web Service using CXF framework.

The example in this case is an InterestRate Service.

Tools / technologies

Version

CXF

2.1

Maven

2.0

Tomcat

apache-tomcat-6.0.24

JDK

java version 1.6.0_20


 


 


 

The Application Scope

The "Interest Rate Service" application demonstrates how easily you can develop a Web Service using CXF frame work. The WSDL service definition defines three operations. Below is the InterestRateService interface.


 

@WebService

public
interface
InterestRateService {

    float getInterestRate (MortgageType type, int term );

    Collection <MortgageType> getMortgageTypes ();

    void addMortgageType (MortgageType type);

}

Building the Web Service

  1. Create a Maven project as below:

    mvn archetype:create -DgroupId=com.mycompany.webservice -DartifactId=rateservice -DarchetypeArtifactId=maven-archetype-webapp

  2. Create implementation class for InterestRateService. See the a portion of the implementation.


 

/*import statements */


 

@WebService(endpointInterface = "com.mycompany.webservice.service.InterestRateService")

public
class InterestRateServiceImpl implements InterestRateService {

    List<MortgageType> mortgageTypes = new ArrayList<MortgageType>();

    

    public
float
getInterestRate(MortgageType type,
int term) {

        float
rate = 0.0f;


 

        if (type.equals("CLOSED")) {

            if (term == 1) {

                rate = 3.9f;

            } else
if (term > 1 && term < 5) {

                rate = 4.0f;

            } else
if (term >= 5 && term < 10) {

                rate = 4.9f;

            } else
if (term > 10) {

                rate = 5.9f;

            }

        } else
if (type.equals("OPEN")) {

            rate = 6.5f;

        } else
if (type.equals("CONVERT")) {

            rate = 4.9f;

        } else
if (type.equals("CASH-BACK")) {

            rate = 6.1f;

        }

        return rate;

    }


 

Define your Web.xml. Note the definition of appContext.xml to define a Spring application context. Also see the how the servlet is defined.


 

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

    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="services"
version="2.5">


 

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/appContext.xml</param-value>

    </context-param>

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

    <servlet>

        <servlet-name>Servlet</servlet-name>

        <servlet-class>

            org.apache..transport.servlet.CXFServlet

        </servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>CXFServlet</servlet-name>

        <url-pattern>/webservices/*</url-pattern>

    </servlet-mapping>

</web-app>

Define your appContext.xml where you can hook up to spring instantiated services.

<?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:context="http://www.springframework.org/schema/context"

    xmlns:cxf="http://cxf.apache.org/core"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://cxf.apache.org/core

http://cxf.apache.org/schemas/core.xsd

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd"

    default-autowire="byName">


 

    <!-- Load CXF modules from cxf.jar -->

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

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

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


 

    <!-- Enable message logging using the CXF logging feature -->

    <cxf:bus>

        <cxf:features>

            <cxf:logging
/>

        </cxf:features>

    </cxf:bus>


 

    <!-- The service bean -->

    <bean
id="interestRateServiceImpl"

        class="com.mycompany.webservice.service.InterestRateServiceImpl"
/>


 

    <!-- Aegis data binding -->

    <bean
id="aegisBean"

        class="org.apache.cxf.aegis.databinding.AegisDatabinding"

        scope="prototype"
/>


 

    <bean
id="jaxws-and-aegis-service-factory"

        class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"

        scope="prototype">

        <property
name="dataBinding"
ref="aegisBean"
/>

        <property
name="serviceConfigurations">

            <list>

                <bean

                    class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"
/>

                <bean

                    class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"
/>

                <bean

                    class="org.apache.cxf.service.factory.DefaultServiceConfiguration"
/>

            </list>

        </property>

    </bean>


 

    <!-- Service endpoint -->


 

    <jaxws:endpoint
id="interestRateService"

        implementorClass="com.mycompany.webservice.service.InterestRateServiceImpl"

        implementor="#interestRateServiceImpl"
address="/interestRate">

        <jaxws:serviceFactory>

            <ref
bean="jaxws-and-aegis-service-factory"
/>

        </jaxws:serviceFactory>

    </jaxws:endpoint>


 

</beans>

Build the application. Here is the pom.xml I used to build the application.

<?xml
version="1.0"
encoding="UTF-8"?>

<project>

    <modelVersion>4.0.0</modelVersion>


 

    <packaging>war</packaging>

    <version>1.0</version>

    <name>rateservice</name>

    <groupId>com.mycompany.webservice</groupId>

    <artifactId>rateservice</artifactId>

    
 

    <properties>

        <cxf.version>2.1.1</cxf.version>

        <spring.version>2.5.5</spring.version>

    </properties>


 

    <dependencies>

        <!-- Depending on your requirements you may need more or fewer modules from cxf -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring</artifactId>

            <version>${spring.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>${spring.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${spring.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-beans</artifactId>

            <version>${spring.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

            <version>${spring.version}</version>

        </dependency>

                

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-core</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-simple</artifactId>

            <version>${cxf.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-databinding-aegis</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-local</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>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-http-jetty</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-jms</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-management</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-common-utilities</artifactId>

            <version>${cxf.version}</version>

        </dependency>

        <dependency>

            <groupId>org.mortbay.jetty</groupId>

            <artifactId>jetty</artifactId>

            <version>6.1.6</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.2</version>

            <scope>test</scope>

        </dependency>

    </dependencies>


 

    <build>

        <plugins>

            <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                    <source>1.5</source>

                    <target>1.5</target>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.mortbay.jetty</groupId>

                <artifactId>maven-jetty-plugin</artifactId>

                <version>6.1.8</version>

                <configuration>

                    <contextPath>rateservice</contextPath>

                </configuration>

            </plugin>

        </plugins>

    </build>


 

</project>


 

Now deploy the rateservice.war

You will be able to see the web service as below:


 

http://localhost:8080/rateservice-1.0/webservices/interestRate?wsdl


 

The service methods can be hit as below. For example, the below url shows all the mortgageTypes.


 

http://localhost:8080/rateservice-1.0/webservices/interestRate/getMortgageTypes


 

You may download the complete source code from


 


 

Comments

Anonymous said…
source code download not working..
Anonymous said…
This is a semi complete explanation and author doesn't care to respond to queries, I wonder why google lists this site 3rd in the search results

Popular posts from this blog

SAAS Simple Maturity Model

There are two architectural models – commonly referred as SAAS Maturity models- that describe the transition of a service to what is called Multi-tenant efficient, highly scalable application. The SAAS Maturity model described by Microsoft has four distinct stages and is illustrated below. Another similar well-known model for SaaS-maturity is known as Forrester-model but adds another stage known as "Dynamic Business Apps-as-a-service". The three key Attributes of a SAAS Architecture: Configurability: Metadata used to configure the way the application behaves for customers Multi-tenant Efficiency : Maximizing the sharing of resources across tenants Scalability: Maximizing concurrency, resource efficiency SAAS Simple Maturity Model (Microsoft, 2006) SaaS Maturity Model (Forres...

CXF – Webservice Example – Contract first

We will use the same WSDL document that we used in our earlier example. The contract is given below: Step: 1 Run the wsdl2java tool to generate the requires service class, JAXB input output message classes. wsdl2java.bat -d src -ant -impl -server rateService.wsdl This command will generate: JAXB Input and Output message classes, Service Interface, Service Implementation class, Standalone Server class and an ant build file to build the service. If you open up these files in the editor, then you see various JAXB annotations on classes and methods which are used to map a Java class to XML. It will also generate Request and Response Wrapper classes as with the JAX-WS specification. It also generate Request and Response Wrapper classes as with the JAX-WS specification ...