Entity Provider Example

This example demonstrates how to add support for custom Java types as resource class method parameters.

Contents

The example consists of seven Java classes:

com.sun.jersey.samples.entityprovider.Main
Contains the main function which starts and stops the HTTP server.
com.sun.jersey.samples.entityprovider.resources.MapResource
A resource class that provides read and write access to a Hashtable.
com.sun.jersey.samples.entityprovider.resources.PropertiesResource
A resource class that provides read access to the system properties as plain text.
com.sun.jersey.samples.entityprovider.resources.NameValuePair
A utility class used to update MapResource. The input HTML form data is converted into an instance of this class by FormReader.
com.sun.jersey.samples.entityprovider.FormReader
A message body reader that can read HTML form data and produce an instance of NameValuePair. This class is used to instantiate the NameValuePair parameter of MapResource.updateDataItem().
com.sun.jersey.samples.entityprovider.FormWriter
A message body writer that can write an HTML page that contains the contents of a Hashtable along with a HTML form for modifying the contents. This class is used to render the output of MapResource.getData() and MapResource.updateDataItem().
com.sun.jersey.samples.entityprovider.PropertiesProvider
A message body writer that can write a plain text representation of a Properties instance. This class is used to render the output of PropertiesResource.getSystemProperties().

Jersey uses the following files to find the message body readers and writers (FormReader, FormWriter and PropertiesProvider):

META-INF/services/javax.ws.rs.ext.MessageBodyReader
Lists each class that implements MessageBodyReader (FormReader).
META-INF/services/javax.ws.rs.ext.MessageBodyWriter
Lists each class that implements MessageBodyWriter (FormWriter and PropertiesProvider).

Annotations on each message body reader and writer indicate the supported media types.

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP methods
/data MapResource GET, POST
/properties PropertiesResource GET

Running the Example

Run the example as follows:

mvn compile exec:java

From a web browser, visit:

http://127.0.0.1:9998/properties

From a web browser, visit:

http://127.0.0.1:9998/data

Fill in the form and submit it. Note that the list below the form is updated each time the form is submitted.

A WADL description may be accessed at the URL:

http://127.0.0.1:9998/application.wadl