Application Overview application
This sample application demonstrates a simple order system which can get, delete, and edit ordering data using RESTful Web services. This RESTful Web services application works at XML message level. It uses the Asynchronous JavaScript and XML (Ajax) technique to combine Java technologies, XML, and JavaScript for the web service applications to uses client-side scripting to exchange data with the server. When a user creates a new order or edits an existing one, the resource data is converted into XML schema for processing. This web service is exposed as a Servlet in the Geronimo application server. See RESTful Web services for more information on the concept.
Application contents
The restfulorder-javaee6 application is deployed as a WAR to the application server. The overview of the structural content of this WAR file is given as follows.
The org.apache.geronimo.samples.javaee6.restfulorder
package consists of the following files.
where,
- The service folder contains the resource classes of the RESTful application.
- The config folder contains the servlet class that extends
javax.ws.rs.core.Application
to configure the application classes within a JAX-RS runtime. Thejavax.ws.rs.core.Application
class provides methods that can specify a set of root resources and providers accessible from a specified application path. This way users can specify several groups of resources in a single application. - The converter folder contains Java Architecture for XML Binding (JAXB) annotated classes to support the processing of XML in requests and responses.
- The entities folder contains an entity class that represents the table of order information in the database.
When running the restfulorder-javaee6 application, index.html is launched as the welcome-file as specified in web.xml. From index.html, showAll.html is called. By clicking the Add a new order link on showAll.html, you are linked to addOrder.html. On this page, you can create a new order by inputting data into the fields. The order data is received by a Restfulorder JavaScript object which provides methods to record and configure the order resource into String
type. Then, an addItem
method provided by Restfulorders.js is called to add the new order resource into the database. The working process is as follows:
- The
addItem
method calls apostJson
method to send aXMLHttpRequest
containing the resource data inapplication/json
type to the servlet class ApplicationConfig. - The
XMLHttpRequest
arrives at the servlet just like aHttpServletRequest
. After parsing the request parameters, the servlet will invoke the necessary application logic. For this application, the request methods from RestfulordersResource and RestfulorderResource classes are called according to the request type. - The request methods handle resource data at XML message level by adopting a JAXB object instance of RestfulorderConverter or RestfulordersConverter to process resource data in XML format.
- The order data is recorded into the database by accessing the Restfulorder entity class with a RestfulorderConverter or RestfulordersConverter object instance.
Application implementation
The postJson
method, called to send a XMLHttpRequest
to the servlet class, uses the Ajax technique provided in the following JavaScript support.js.
where,
XMLHttpRequest
is a JavaScript object used to send HTTP or HTTPS requests directly to a web server and load the server response data directly back into the script. In this application, anXMLHttpRequest
instancexmlHttpReq
is created to help a client-side script perform HTTP requests. The HTTP method to use for the request, the destination URL, and the request content type are set on theXMLHttpRequest
object with theopen
function when initializing the request.open : function(method2, url2, mimeType, paramLen, async)
is the function that opens a connection to the server resource at the specified URL (url2
) for the specified method (method2
). This function initializes the HTTP and HTTPS requests of theXMLHttpRequest
object and must be invoked prior to the actual sending of a request.setRequestHeader
is a method of theXMLHttpRequest
object that is used to set HTTP headers to be send with the request.get
,post
,put
anddelete
are functions responsible for dispatching requests. Each function first obtains an instance ofXMLHttpRequest
and initialize it withopen
. Then the requests are sent to the web server and will arrive on the server just like any other HttpServletRequest.send
is the function used to send requests to the web server. It accepts a single parameter that can contain the content to be sent with the request. The requests perform like HttpServletRequests on the web server and after parsing the request parameters, the servlet will invoke the necessary application logic. For this application, theget
,post
,put
anddelete
request methods from RestfulordersResource and RestfulorderResource classes are called according to the request type.
ApplicationConfig is a servlet that extends the javax.ws.rs.core.Application
class to configure the application classes within a JAX-RS runtime.
where,
@javax.ws.rs.ApplicationPath
is an annotation that helps to specify the resource path of the application. The value of this annotation is used as the servlet URL pattern. For example, the URI for the resource presented in this application can be http://localhost:8080/restfulorder-javaee6/resources/restfulorders/, where /resources is the servlet URL. You can also specify this URL patterns in yourweb.xml
file if this annotation is absent.javax.ws.rs.core.Application
is a class that provides thegetClasses
andgetSingletons
methods that can specify a set of root resources and providers accessible from a specified application path respectively. The ApplicationConfig class uses the default implementations ofjavax.ws.rs.core.Application
subclassgetClasses
andgetSingletons
methods that return empty sets. In this case, when both methods return empty sets, all root resource classes and providers packaged in the application are included in the published JAX-RS application.
RestfulordersResource is a resource class that uses JAX-RS annotations to implement the corresponding Web resource. The request methods defined in this class are called when the server receives the corresponding HTTP requests.
where,
@Path("/restfulorders/")
is the annotation that defines the relative URI to your resource. The base URI is provided by the combination of your hostname, port, application context root, and any URI pattern mappings in the application'sweb.xml
file. For example, the URI for the resource presented by this resource class can be http://localhost:8080/restfulorder-javaee6/resources/restfulorders/.@Path("{id}/")
annotates a sub-resource locator which returns aRestfulorderResource
object to handle HTTP requests. Theid
parameter in this annotation defines the relative URI under/restfulorders/id
to the resource.@Context
is used in this resource class to inject an instance ofUriInfo
into the class field.UriInfo
instances can provide information on the components of a request URI.@GET
and@POST
are request method designators for annotating resource methods in a resource class. Other common request method designators include@PUT
,@DELETE
, and@HEAD
.@Consumes
and@Produces
annotations are used to declare the supported request and response media types. In this resource class, thepost
andget
methods both supportapplication/xml
andapplication/json
.
RestfulordersConverter is a Java Architecture for XML Binding (JAXB) annotated class that support the processing of XML in requests and responses.
where,
@XmlRootElement(name = "restfulorders")
is an annotation that maps the class to the root of the XML document. In this application, when the user creates a new order with a unique id, for example 01, the XML document containing the corresponding data is located at http://localhost:8080/restfulorder-javaee6/resources/restfulorders/01.
Get the source code
Please refer to Samples General Information for informations on obtaining and building the source for this and other samples.
Build the RESTful Web services application
Once all the sources get checked out the next step is to build restfulorder-javaee6 sample. It requires Maven 2 or above for building the binaries.
From the <restfulorder-javaee6_home> directory run the following command.
mvn clean install
This process will take a couple of minutes. The binaries will be generated in the corresponding target directory .
Deploy the RESTful Web services application
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
- Scroll down to Deploy New from the Console->Navigation panel.
- Load restfulorder-javaee6-war-version.war from restfulorder-javaee6-war\target folder in to the Archive input box.
- Press Install button to deploy application in the server.
Test the RESTful Web services application
The application is visible at http://localhost:8080/restfulorder-javaee6.
Click the Add a new order link to create a new order.
Click Save after inputting all the data for your order. You will see the added order on the next page.
You can also see the order resource at http://localhost:8080/restfulorder-javaee6/resources/restfulorders/id in XML format, where id is the unique id value you defined for your order.