2. AboutTHisPresentation
This presentation will focus on:
● RESful Web Service
● JSON Format - No detail about XML or other format message transformation
● JAX-RS standard - No detail about JAX-WS
● Apache CXF
● JBoss Fuse 6.2
4. RESTfulWebServices- INTRODUCTION
Representational State Transfer(REST)
● a software architecture style that centers around the transmission of data
over HTTP, using only the four basic HTTP verbs.
● everything is a resource
● The state/data of the resource can be retrieved, changed, deleted commonly
using HTTP verbs (GET, POST, PUT, DELETE, etc.)
● resource identified by Uniform Resource Identifiers (URIs), for example
/people/unyil
7. CommonlyURI&HTTPMethodUsage
Resource GET PUT POST DELETE
Collection URI, such as
http://api.example.com/resources/
List the URIs and perhaps
other details of the
collection's members.
Replace the
entire collection
with another
collection.
Create a new entry in the
collection. The new entry's
URI is assigned
automatically and is usually
returned by the operation.[11]
Delete the
entire
collection.
Element URI, such as
http://api.example.com/resources/item17
Retrieve a representation
of the addressed member
of the collection,
expressed in an
appropriate Internet media
type.
Replace the
addressed
member of the
collection, or if it
does not exist,
create it.
Not generally used. Treat the
addressed member as a
collection in its own right
andcreate a new entry in it.
[11]
Delete the
addressed
member of
the
collection.
8. HTTPMethod
Method Operation performed on server Quality
GET Read a resource. Does not have any effect on
the original value of the
resource
PUT Insert a new resource or update if the
resource already exists.
Gives the same result no
matter how many times you
perform it
POST Insert a new resource. Also can be
used to update an existing resource.
N/A
DELETE Delete a resource . Gives the same result no
matter how many times you
perform it
OPTIONS List the allowed operations on a
resource.
Does not have any effect on
the original value of the
resource
HEAD Return only the response headers and
no response body.
Does not have any effect on
the original value of the
resource
9. HTTPResponseCode
200 OK
General success status code. This is the most common code. Used to indicate success.
201 CREATED
Successful creation occurred (via either POST or PUT). Set the Location header to contain a link to the newly-created resource
(on POST). Response body content may or may not be present.
204 NO CONTENT
Indicates success but nothing is in the response body, often used for DELETE and PUT operations.
400 BAD REQUEST
General error for when fulfilling the request would cause an invalid state. Domain validation errors, missing data, etc. are some
examples.
401 UNAUTHORIZED
Error code response for missing or invalid authentication token.
403 FORBIDDEN
Error code for when the user is not authorized to perform the operation or the resource is unavailable for some reason (e.g.
time constraints, etc.).
404 NOT FOUND
Used when the requested resource is not found, whether it doesn't exist or if there was a 401 or 403 that, for security reasons,
the service wants to mask.
10. HTTPResponseCode
405 METHOD NOT ALLOWED
Used to indicate that the requested URL exists, but the requested HTTP method is not applicable. For example, POST
/users/12345 where the API doesn't support creation of resources this way (with a provided ID). The Allow HTTP header must
be set when returning a 405 to indicate the HTTP methods that are supported. In the previous case, the header would look like
"Allow: GET, PUT, DELETE"
409 CONFLICT
Whenever a resource conflict would be caused by fulfilling the request. Duplicate entries, such as trying to create two customers
with the same information, and deleting root objects when cascade-delete is not supported are a couple of examples.
500 INTERNAL SERVER ERROR
Never return this intentionally. The general catch-all error when the server-side throws an exception. Use this only for errors
that the consumer cannot address from their end.
12. JSONoverXML
● Still readable by human
● Simple
● JSON does not have many concepts found in XML such as namespaces,
attributes or entity references
● Support interface definition? YES
○ WADL
○ Swagger
○ RAML
○ API Blueprint
16. RESTfulWebServiceinJBossFuse
● RESTful Web Service is supported in JBoss Fuse by Apache CXF component
● CXF implement JAX-RS standard
● CXF can be used as REST Client or Server
17. ApacheCFX
CXF provides:
● Java API for building Web Service using JAX-WS
○ Generating WSDL from Java classes and generating Java classes from WSDL
○ WS-* standards support e.g. WS-Addressing, WS-Policy, WS-ReliableMessaging and
WS-Security
○ etc
● Java API for RESTful Web Services JAX-RS 2.0 (JSR-339), JAX-RS 1.1 (JSR-
311)
○ Generating WADL from services, generating Java Interface from WADL
● Can be embedded in standalone app, used in Java EE Server app or in OSGi
engine (JBoss Fuse)
18. RESTfulWebServicesinJBossFuse
Can be developed WITH or WITHOUT Camel
● WITH Camel
○ Good for RESTful-ized a non JAX-RS Java Class
○ URI defined in config file (blueprint.xml)
○ Options:
■ Using CXFRS component (http://camel.apache.org/cxfrs.html)
■ Using REST wrapper layers
● REST DSL (in camel-core)
● Rest component (in camel-core)
● WITHOUT Camel:
○ Good for Atomic Service
○ No Routing (Integration flow)
○ Create a RESTful (JAX-RS) web service using CXF and expose it with the OSGi HTTP
Service.
○ URI, HTTP method is defined in Java Class using Anotation
○ Quickstart project: https://github.com/jboss-fuse/quickstarts/tree/5715944/rest
21. JAX-RS
1. The root URI for the resources exposed
by the service
2. Public constructor
3. HTTP verbs
4. Sub-resource, URI for the sub-resource,
as specified using the the @Path
annotation, is
customerservice/order/id
22. Install&RunFuse
Before building and running this quick start you need:
● Maven 3.0.4 or higher
● JDK 1.6 or 1.7
● JBoss Fuse 6.2.1
unzip ~/RH_JBOSS_INSTALLER/Fuse_6.2.1/jboss-fuse-full-6.2.1.redhat-084.zip -d /Servers/
sed -i.orig 's/#admin/admin/g' /Servers//jboss-fuse-6.2.1.redhat-084/etc/users.properties
/Servers/jboss-fuse-6.2.1.redhat-084/bin/start
tail -f /Servers/jboss-fuse-6.2.1.redhat-084/data/log/fuse.log
$FUSE_HOME=/Servers/jboss-fuse-6.2.1.redhat-084
1. Install Fuse 6.2.1
2. Uncomment line “#admin” (remove “#”) to
set a username and password
3. Start Fuse
4. See the log file
25. ImportQuickstartProject
● Open Eclipse (JBDS), Create new Workspace
● File > Import, “Existing Maven Projects”, Next
● Root Directory: “/Servers/jboss-fuse-6.2.1.redhat-084/quickstarts/”
● Next, Finish
26. Eclipse/JBDSSetting
● Help > Preferences
● Type “Maven” in the search textbox
● Select Errors/Warning, change “Plugin execution…” to “Ignore”
27. Eclipse/JBDSSetting
● Help > Preferences
● Type “Maven” in the search textbox
● JBoss Maven Integration > “Configure Maven Repositories..”
Maven repo: https://repository.jboss.org/nexus/content/groups/ea/
28. ProjectStructure
Some POJOs
(Business Objects)
RESTful Service
OSGi Configuration File
Maven Configuration File
For real project, better to
separate Business Object and
Service classes in different
Maven Project
Navigate to Package Explorer view, and expand “cfx-rest” project
31. OSGI-INF/bluerprint/blueprint.xml
Define the Service
Provider bean to be
able to used by other
Documentation:
JAX-RS Endpoint: https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.2.1/html/Apache_CXF_Development_Guide/JAXRSEndpointConfig.html
http://cxf.apache.org/docs/jaxrs-services-configuration.html#JAXRSServicesConfiguration-Blueprint
Create a REST server
address: http://hostname:
port/cfx/crm
Create a REST server
address: http://hostname:
port/cfx/crm
32. blueprint.xml
XML Namespace for JAX-RS using CXF
Prefix Namespace
(default) http://www.osgi.org/xmlns/blueprint/v1.0.0
cxf http://cxf.apache.org/blueprint/core
jaxrs http://cxf.apache.org/blueprint/jaxrs
37. InstallNewBundle(Application)
OSGi > Bundles
Textbox for Maven
library
MAVEN REPOSITORY DIRECTORY:
Files as a result of Maven build
and install command:
`mvn clean install`
Click this to deploy application
(cxf-res-6.2.1.redhat-084.jar)
39. BundleDetailInformationandActions
OSGi > Bundles - Click “JBoss Fuse Quickstart: rest”
Action Button:
Stop, Start, Refresh,
Update, Delete
Bundle Information
● Maven Repo location
● Some info from Maven POM file
● Start level: order number when
Fuse starting
OSGi Service
Additional info