Introduction:
Fuse ESB Enterprise's embedded Web and
RESTful services framework is based on Apache CXF. They provide a small
footprint engine for creating reusable web services and for service-enabling
new and legacy applications as part of an integration solution.
RESTful Service
Development Pattern
Overview
Representational
State Transfer (REST) describes a stateless client-server architecture in which
web services are treated as resources that can be identified by their URLs.
In RESTful
systems, servers use URIs to expose resources, and clients use the four HTTP
verbs
GET
, POST
, PUT
, and DELETE
to access
them. As they receive a representation of a resource, client applications enter
a state. When they access another resource, typically by following a link,
client applications change their state.
Because
RESTful systems can take full advantage of HTTP's scalability features, such as
caching and proxies, they are highly scalable and highly flexible. Changes made
to servers do not impact clients because:
·
Resources are accessed and manipulated using the four HTTP verbs.
·
Resources are exposed using a URI.
·
Resources are represented using standard grammars.
Today's Web
architecture is an example of a system designed on RESTful principles. Web
browser clients access resources hosted on Web servers. The resources are
represented using HTML or XML grammars understood and navigable by all web
browsers.
JAX-RS development
Fuse ESB
Enterprise supports JAX-RS (JSR-311), the Java API for RESTful Web Services.
JAX-RS provides a standard way to build RESTful services in Java, using
annotations to simplify the development and deployment of web service clients
and endpoints.
Using JAX-RS
to develop restful services has several benefits:
·
The URI bindings are local to the resource beans, which can be
arbitrarily nested. This feature simplifies refactoring.
·
Loose coupling between providers and the objects returned by the
resource methods makes it easy to drop in support for new representations,
without having to change the code of the resource beans (or controllers). Instead,
you need only modify an annotation.
·
Static typing can be useful when binding URIs and parameters to
the controller. For example, using
String
, integer
, and Date
fields frees the controller from having to
explicitly convert parameter values.
JSON data bindings
JavaScript
Object Notation (JSON), a lightweight data format for data exchange, is
provided as an alternative to JAXB. JSON is a text-based and human-readable
format for representing simple data structures and associative arrays (called
objects).
Implementation:
Suppose you
want to create a RESTfull web service using CXF for hello world. You
would typically want to perform following operations.
Ø
Create Fuse Project
Ø
Create Java classes
Ø
Create Camel Context
Ø
Deploy in Fuse Server
Ø Test Hello world
Create Fuse Project:
Step 1: Go to Fileà New à Fuse Project.
Step 2: Give Project
name as ‘FuseHelloWorldRSService’ and click on next.
Step 3: Select the
artifact ID: camel-archetype-spring-dm
and Group ID: org.apache.camel.archetypes
and give the details as shown below and click on finish.
Create Java classes
Step 4: Create HelloWorldReqest , HelloWorldResponse, HelloWorldImpl
and HelloWorldResponseTransformer classes and generate
setter and getter methods.
Step 5: Open POM.xml
fine and add the following dependencies, so that incase of any errors in our
Java classes will resolve these dependencies.
Create Camel Context
Step 6: Open the
blueprint.xml under camel Contexts folder. Delete the content on blueprint.xml
Step 7: Click on source
to edit the blueprint.xml file and add the schema locations.
Step 8: Add Bean
entries for classes and service endpoint to blueprint.xml.
Step 9: Add the routes
in blueprint.xml
Deploy in Fuse Server
Step 10: Right click on
pom.xml à Run As à Maven Install
Once the build
successful we can see the success build message in console.
Step 11: Start Fuse
server. Run the below command in Fuse server command prompt.
osgi:install
-s mvn:com.eai.helloworldrs/helloworldrs/1.0.0-SNAPSHOT
Run the list command to
check the status of the service. You can see below the service is in run status
with Bundle ID 273.
Test Hello world
Now our service is
running on Fuse server. We need to test now.
Open Mozilla Browser
and Install the Rest Client plug-in which is under Mozilla plug-in’s. This plug
in is used for test the rest services.
Enter the ‘http://localhost:9090/getHelloWorld/getHelloWorld?name=Ameer’
under url, select as GET method and click on Send. We can see the response
under Response Body(Raw) tab as shown below.
Summary:
We have learnt how to develop simple hello
world program with restful services with Fuse. I will soon post new post with
advance usage/ implementations with restful services in Fuse.
Downlad:
S. No
|
File Name
|
Download
|
1
|
Developing HelloWorld Restful Service.pdf
|
|
2
|
FuseHelloWorldRSService.zip
|