Application Overview
This sample application demonstrates a simple order system which can get, delete, and edit resources using RESTful Web services. 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 the WAR file is given as follows.
|-restfulorder-javaee6.war +-rest +- restfulorder +-WEB-INF +- web.xml +- classes +- org.apache.geronimo.samples.javaee6.restfulorder +- META-INF +-META-INF
The org.apache.geronimo.samples.javaee6.restfulorder
package consists of the following files.
+- org.apache.geronimo.samples.javaee6.restfulorder +-service +- RestfulordersResource.class +- RestfulorderResource.class +-config +- ApplicationConfig.class +-converter +- RestfulorderConverter.class +- RestfulordersConverter.class +-entities +-JPASessionBeans
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 annotated classes to support the processing of XML in requests and responses.
Application implementation
RestfulordersResource is a resource class that uses JAX-RS annotations to implement the corresponding Web resource.
package org.apache.geronimo.samples.javaee6.restfulorder.service; import java.util.Collection; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.DefaultValue; import javax.ws.rs.core.Response; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.persistence.EntityManager; import org.apache.geronimo.samples.javaee6.restfulorder.converter.RestfulordersConverter; import org.apache.geronimo.samples.javaee6.restfulorder.converter.RestfulorderConverter; import javax.persistence.PersistenceContext; import javax.ejb.Stateless; import org.apache.geronimo.samples.javaee6.restfulorder.entities.Restfulorder; @Path("/restfulorders/") @Stateless public class RestfulordersResource { @javax.ejb.EJB private RestfulorderResource restfulorderResource; @Context protected UriInfo uriInfo; @PersistenceContext(unitName = "RestfulProduct2PU") protected EntityManager em; /** Creates a new instance of RestfulordersResource */ public RestfulordersResource() { } /** * Get method for retrieving a collection of Restfulorder instance in XML format. * * @return an instance of RestfulordersConverter */ @GET @Produces({"application/xml", "application/json"}) public RestfulordersConverter get(@QueryParam("start") @DefaultValue("0") int start, @QueryParam("max") @DefaultValue("10") int max, @QueryParam("expandLevel") @DefaultValue("1") int expandLevel, @QueryParam("query") @DefaultValue("SELECT e FROM Restfulorder e") String query) { return new RestfulordersConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel); } /** * Post method for creating an instance of Restfulorder using XML as the input format. * * @param data an RestfulorderConverter entity that is deserialized from an XML stream * @return an instance of RestfulorderConverter */ @POST @Consumes({"application/xml", "application/json"}) public Response post(RestfulorderConverter data) { System.out.println("before resolve,in post"); Restfulorder entity = data.resolveEntity(em); createEntity(data.resolveEntity(em)); return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build(); } /** * Returns a dynamic instance of RestfulorderResource used for entity navigation. * * @return an instance of RestfulorderResource */ @Path("{id}/") public RestfulorderResource getRestfulorderResource(@PathParam("id") Integer id) { restfulorderResource.setId(id); restfulorderResource.setEm(em); return restfulorderResource; } /** * Returns all the entities associated with this resource. * * @return a collection of Restfulorder instances */ protected Collection<Restfulorder> getEntities(int start, int max, String query) { return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList(); } /** * Persist the given entity. * * @param entity the entity to persist */ protected void createEntity(Restfulorder entity) { em.persist(entity); } }
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
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
.
ApplicationConfig is a servlet that extends the javax.ws.rs.core.Application
class to configure the application classes within a JAX-RS runtime.
package org.apache.geronimo.samples.javaee6.restfulorder.config; @javax.ws.rs.ApplicationPath("resources") public class ApplicationConfig extends javax.ws.rs.core.Application { }
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/. You can also specify this URL patterns in your
web.xml
file if this annotation is absent.javax.ws.rs.core.Application
is a class that contains 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 of javax.ws.rs.core.Application subclassgetClasses
andgetSingletons
methods that return empty sets. In this case, when both methods return an empty list, all root resource classes and providers packaged in the application are included in the published JAX-RS
application.