This example demonstrates how to develop RESTful web services with a Servlet 2.5 container.
The example consists of five Java classes, one HTML file, and one JPEG image:
com.sun.jersey.samples.servlet.resources.MasterResourceBean
com.sun.jersey.samples.servlet.resources.ResourceBean1
com.sun.jersey.samples.servlet.resources.ResourceBean2
com.sun.jersey.samples.servlet.resources.ResourceBean3
com.sun.jersey.samples.servlet.resources.ResourceBean4
index.html
java.jpg
The mapping of the URI path space is presented in the following table:
URI path | Resource class | HTTP methods |
---|---|---|
/start | MasterResourceBean | GET |
/resource1 | ResourceBean1 | GET |
/resource2 | ResourceBean2 | GET |
/resource3/{arg1}/{arg2} | ResourceBean3 | GET |
/resource4 | ResourceBean4 | GET |
Run the example as follows:
run
mvn glassfish:run
From a web browser, visit:
http://localhost:8080/SimpleServlet/resources/start
Select the resource to test in the form and submit it by clicking the Test Resource button. If you select Resource 1 or Resource 2, a Test Details section appears on the form showing what the requested URL was, any Query string used, the value of the "Accept" header used, the HTTP headers received on the result, and the content of the result. Here is a sample of what you would see if you select Resource 1:
Test Details:If you select Resource 3, a Resource 3 Inputs section of the form appears. Param 1 and Param 2 are URI parameters that are used in the result of testing the "text/plain" or "application/x-www-form-urlencoded" MimeType. The Selection method: lets you choose whether the "rep" query parameter or the "Accept" HTTP header should be used to retrieve the result. After filling in the Resource 3 Input, click the Test Resource 3 button to see the Test Details.
Resource: Resource 1
Request: /endpoint/resource1 Query:
Accept MimeType:
Result Headers: Last-Modified: Tue, 09 Jan 2007 20:59:59 GMT Content-Type: text/plain Transfer-Encoding: chunked Result: Hello World from resource 1
You can exercise ResourceBean1 directly:
http:<host:port>//SimpleServlet/resources/resource1
You can exercise ResourceBean2 directly:
http:<host:port>//SimpleServlet/resources/resource2
From a web browser, you can only test the query parameter portion of ResourceBean3.
You must provide two URI parameters and the rep
query parameter.
The following URLs can be used:
http:<host:port>//SimpleServlet/resources/resource3/ramesh/rajiv?rep=0
http:<host:port>//SimpleServlet/resources/resource3/dave/steve?rep=1
http:<host:port>//SimpleServlet/resources/resource3/bill/john?rep=2
http:<host:port>//SimpleServlet/resources/resource3/marc/paul?rep=3
You can exercise ResourceBean4 directly:
http:<host:port>//SimpleServlet/resources/resource4