About this task
The JAX-RS Section on the Apache CXF website provides a solid background to implementing REST services and also provides the latest information on the newest RESTful features offered by CXF.
For an example of working with a RESTful application in Eclipse and deploying the service to either Tomcat or the Talend Runtime Container, let's look at the JAXRS-Intro sample provided in the examples/talend folder of the Talend ESB installation. The demo lists the Persons who are part of a generic membership, and allows GETs to retrieve a single member or all members, POSTs to add members, and PUTs for updates to membership information.
The JAX-RS Server provides one service via the registration of a root resource class, MembershipService which relies on within-memory data storage. MembershipService provides a list of its members, which are individual Person objects containing name and age. New persons can be added to the MembershipService, and individual members can have their information updated. The RESTful client uses CXF JAX-RS WebClient to traverse all the information about an individual Person and also add a new child.
This sample consists of four subfolders:
|client||This is a sample client application that uses the CXF JAX-RS API to create HTTP-centric and proxy clients and makes several calls with them.|
|common||This directory contains the code that is common for both the client and the server. POJOs and the REST interface is kept here.|
|service||This is the JAX-RS service holding the Membership root resources packaged as an OSGi bundle.|
|war||This module creates a WAR archive containing the code from common and service modules.|
Working with a REST sample in Eclipse
From a command-line windows, navigate to the jax-rs folder and type
mvn clean install eclipse:eclipse. This will create an Eclipse project out of this sample that we can import into Eclipse.
From Eclipse we can now import the project. From the Menu row, select File : Import..., and from the resulting Import popup, choose
Existing Projects into Workspace (see
illustration below). Select Next.
Select the four Eclipse projects comprising this example: jaxrs-intro-client, jaxrs-intro-common,
jaxrs-intro-service-bundle, and jaxrs-intro-service-war. You'll see them listed in the
left-side Eclipse Navigator and Project Explorer views. At this stage any of the files can be viewed and modified. Be sure to
mvn clean installfrom the jaxrs-intro folder within a command prompt window after any changes made.
Prior to running the client, we'll need to activate the REST service, which we can do in at least two ways:
To run the example within Talend ESB, we'll need to create the Karaf features file that contains the definition for this service. First, from a command prompt navigate to the features folder (sibling to jaxrs-intro) and run
mvn clean install. Next, from the command prompt enter
feature:repo-add mvn:com.talend.sf.examples/osgi/1.0/xml/featuresto install the features file followed by
feature:install tsf-example-jaxrs-introto install the JAXRS-Intro service.
To run the example within CXF's internal (Jetty-based) servlet container, navigate to the war folder and run
To run the client, from a command prompt in the jaxrs-intro/client folder, run