Book Image

Apache CXF Web Service Development

By : Naveen Balani, Rajeev Hathi
Book Image

Apache CXF Web Service Development

By: Naveen Balani, Rajeev Hathi

Overview of this book

<p>Apache CXF framework helps you to develop a standards-based programming model and also provides a flexible deployment model for deploying web services. Developing SOAP and RESTful applications can be made easy by using Apache CXF framework. However, getting started with developing web services using the Apache CXF framework is not easy.<br /><br />This is the first book that gives details on how to use the Apache CXF framework for developing SOAP and REST web services. It is a hands-on practical guide that simplifies working with CXF framework as it covers all major aspects with real-world examples. The chapters cover the various CXF features in detail and each has systematic steps with practical, simple examples to implement these features on your web services. <br /><br />The book introduces the Apache CXF framework and its features such as Frontend API, Data Bindings, Transports, Spring-based configuration, and CXF tools. It also has chapters on SOAP and RESTful services. It will help you create RESTful services that support XML as well as the widely accepted Java Script Object Notation (JSON) format. It explains the components of CXF architecture that help developers customize the Apache CXF framework to suit the target application. The book covers both code-first and contract-first approaches for service deployment. You will see how to develop services in a flexible deployment model offered by CXF, unit test them in a stand-alone environment, and finally promote them in an application server environment.<br /><br />The instructions in this book will help developers to build their application according their requirements by using any of the frontends supported by Apache CXF framework. The various CXF frontend APIs covered in this book provide a wide variety of options in developing and deploying your application.<br /><br />The book introduces some advanced concepts such as Interceptors and features that will add extra capability to your service component. It will help you take advantage of different transport features offered by the CXF runtime such as HTTP, HTTP(S), and JMS protocols.<br />Finally, the book mentions various tools that help developers creating web services as well as creating Java and JavaScript-based web services clients which invoke a real-world .NET web service. These tools are standard batch files that can be easily executed from the Windows command shell by following the instructions in the book.</p>
Table of Contents (16 chapters)
Apache CXF Web Service Development
Credits
About the Authors
About the Reviewer
Preface
Index

Using Maven for Build management


Maven is a software tool for build and project management. It uses a construct known as the Project Object Model (POM), which describes the components of your project and dependency to build the project in an XML format. Maven provides various pre-defined tasks, which facilitate build management and allow extensions to add more specific build tasks. Understanding and using Maven effectively is a vast topic, and there are a lot of books dedicated to using Maven effectively. We intend to provide a very short overview on Maven for readers who are not familiar with the tool.

A key concept in Maven is that of artifacts, a packaged archive like a JAR or WAR, which is created by the build and stored in a repository. Maven maintains artifacts in a repository, indexed by Group ID which specifies the group, Artifact ID which specifies the name of the artifact, and Version which specifies the version number of the artifact. For instance, in the case of the CXF JAX-WS component, the group ID is org.apache.cxf, artifact ID is cxf-rt-frontend-jaxws, and the version is 2.2.3 (or the latest). When you build using the Maven tool, a local repository is created for you, typically in your home drive, that is, C:\Documents and Settings\userName\.m2\repository and all the dependent artifacts required for building the project are copied in their respective groups\artifacts\version folder in the local repository. Note that while building the project, the Maven tool first checks if the required artifact exists in the local repository and then looks up the Maven central repository (or the repository specified) to download the artifact from the Internet.

Apache CXF also supports Maven-based build and installation and provides various tasks which simplify CXF application management. The CXF artifacts can be accessed from the Maven central repository itself. The complete release is available at the following location: http://repo1.maven.org/maven2/org/apache/cxf/

The following shows an example of POM, and how dependencies are declared to build applications which use the CXF framework using Maven:

<properties>
  <!-- CXF Version -->   
  <cxf.version>2.2.3</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>

Each dependency is listed as the <dependency> tag, with the <groupId>, <artifactId>, and <version>

Building chapter source code using Maven

We will show how to use Maven for building the source code as part of Chapter 2. The following shows the pom.xml from the Chapter2\orderapp folder of the source code download. Please refer to the inline comments, which are highlighted in bold for an explanation of the tags.

<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>
   <!-- Defines group id for cxfbook -->
   <groupId>com.packtpub.cxfbook</groupId>
   <!-- Name of the artifact -->
   <artifactId>orderapp</artifactId>
   <!-- Packing format. We want to pakacge this as a WAR archive -->
   <packaging>war</packaging>
   <!-- Version for the oderapp arifact -->
   <version>1.0-SNAPSHOT</version>
   <name>orderapp maven webapp</name>
   <properties>
      <!-- Version of CXF. Change this to latets version for building against latest CXF distribution -->
      <cxf.version>2.2.3</cxf.version>
   </properties>
   <build>
         <!--Directory where the source code is located-->
         <sourceDirectory>src</sourceDirectory>
         <resources>
            <resource>
      <!-- Include properties and xml file from src folder-->
               <directory>src</directory>
               <includes>
                  <include>**/*.properties</include>
                  <include>**/*.xml</include>
               </includes>
            </resource>
            </resources>
     <plugins>
       <plugin>
      <!-- Maven Plugin used to build WAR archive-->
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
        <configuration>
        <!-- Directory for Web application-->
        <webappDirectory>webapp</webappDirectory>
        <webResources>
          </webResources>
        </configuration>
      </plugin>
      <plugin>
       <!-- Plugin for compiling Java code -->
      <artifactId>maven-compiler-plugin</artifactId>
         <configuration>
            <!-- Java version for compiling the source code-->
          <source>1.5</source>
           <target>1.5</target>
         </configuration>
         </plugin>
      </plugins>
      <finalName>orderapp</finalName>
   </build>
   <dependencies>
      <dependency>
    <!-- Apache JAX-WS CXF Dependency for WAR and JAX-WS Client-->
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>${cxf.version}</version>
      </dependency>
      <dependency>
          <!-- Apache JAX-WS CXF Dependency for HTTP transport-->
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http</artifactId>
         <version>${cxf.version}</version>
      </dependency>
   </dependencies>
</project>

To build the Chapter 2 source code, navigate to the Chapter2\orderapp folder, and type in the following command:

mvn clean install

You will see the following build output, and the WAR file will be generated in the orderapp/target folder. You can then deploy the WAR file in the Tomcat server.

Alternatively, if you wish to deploy using a standalone web server like Jetty, then you can add the following plugin in the POM file:

  <plugins>
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>maven-jetty-plugin</artifactId>
      <version>6.1.19</version>
    </plugin>

You can run the server by giving the following command:

mvn jetty:run

The previous command will start the WAR on localhost port 8080

Once the WAR file is deployed, run the client, and this will invoke the Order Process web service by typing in the following command from the Chapter2\orderapp folder:

mvn exec:java -Dexec.mainClass=demo.order.client.Client

You will see the following output and "order ID is ORD1234" being printed at the console:

We have thus successfully built, deployed, and executed the code using the Maven tool.