This example demonstrates how to develop RESTful web services with a Servlet 3.0 container, if you are working with Jersey GlassFish update center module installed into your existing GlassFish instance. Otherwise, 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 |
If you are working with Jersey GlassFish update center module installed into your existing GlassFish instance, you will need to follow instructions at the module README file in order to deploy the example.
Otherwise, you can run the example using embedded GlassFish as follows:
run
mvn clean package embedded-glassfish:run
or you can run the example using Jetty as follows:
mvn clean package jetty: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: /SimpleServlet/resources/resource1 Query:
Accept MimeType:
Result Headers: X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Sun Microsystems Inc./1.6) Server: GlassFish Server Open Source Edition 3.1 Content-Type: text/plain Transfer-Encoding: chunked Date: Tue, 22 Mar 2011 03:32:47 GMT Result: Hello World from resource 1 in servlet: 'com.sun.jersey.samples.servlet.resources.MyApplication', path: '/resources'
Debug Log: updatepage: resultheaders str: X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Sun Microsystems Inc./1.6) Server: GlassFish Server Open Source Edition 3.1 Content-Type: text/plain Transfer-Encoding: chunked Date: Tue, 22 Mar 2011 03:32:47 GMT
You can exercise ResourceBean1 directly:
http://localhost:8080/SimpleServlet/resources/resource1
You can exercise ResourceBean2 directly:
http://localhost:8080/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://localhost:8080/SimpleServlet/resources/resource3/ramesh/rajiv?rep=0
http://localhost:8080/SimpleServlet/resources/resource3/dave/steve?rep=1
http://localhost:8080/SimpleServlet/resources/resource3/bill/john?rep=2
http://localhost:8080/SimpleServlet/resources/resource3/marc/paul?rep=3
You can exercise ResourceBean4 directly:
http://localhost:8080/SimpleServlet/resources/resource4