This example demonstrates how to add support for custom Java types as resource class method parameters.
The example consists of seven Java classes:
com.sun.jersey.samples.entityprovider.Main
com.sun.jersey.samples.entityprovider.resources.MapResource
Hashtable
.com.sun.jersey.samples.entityprovider.resources.PropertiesResource
com.sun.jersey.samples.entityprovider.resources.NameValuePair
MapResource
. The input HTML form
data is converted into an instance of this class by FormReader
.com.sun.jersey.samples.entityprovider.FormReader
NameValuePair
. This class is used to instantiate the
NameValuePair
parameter of MapResource.updateDataItem()
.com.sun.jersey.samples.entityprovider.FormWriter
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
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
MessageBodyReader
(FormReader
).META-INF/services/javax.ws.rs.ext.MessageBodyWriter
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 |
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