Posted by
arungupta on December 1, 2008 at 6:30 AM PST
Lets extend the Jersey endpoint (
href="http://blogs.sun.com/arungupta/entry/totd_56_simple_restful_web">TOTD#
56) and client (
href="http://blogs.sun.com/arungupta/entry/totd_57_jersey_client_api">TOTD#
57) such that it can accept a POST request and then invoke it.
- Add a new method to "MyResource.java" from
href="http://blogs.sun.com/arungupta/entry/totd_56_simple_restful_web">TOTD#
56 as:
style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
@POST
@Consumes("application/json")
@Produces("application/json")
public Greeting postIt(Greeting
greeting) {
System.out.println("In POST: " + greeting.greeting);
return greeting;
} |
The first line indicates that the Java method will process HTTP POST
requests. The second and third line shows that the method consumes and
produces JSON data format.
- Add a new method to "AppTest.java" from
href="http://blogs.sun.com/arungupta/entry/totd_57_jersey_client_api">TOTD#
57 as:
style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
public void
testPost() {
Greeting result = createResource().
type("application/json").
post(Greeting.class, new Greeting("yo!"));
assertTrue(result.greeting.equals("yo!"));
} |
The main difference from the "testApp()" method is specifying the MIME
type of the generated outbound request as "application/json".
- Running the test as "mvn test" shows the following output:
style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
Running org.glassfish.samples.AppTest
1 * Out-bound request
1 > GET
http://localhost:8080/helloworld-webapp/webresources/myresource
1 >
1 < 200
1 < X-Powered-By: Servlet/2.5
1 < Transfer-Encoding: chunked
1 < Content-Type:
text/plain
1 < Server: GlassFish/v3
1 < Date: Tue, 25 Nov 2008 20:19:34 GMT
1 <
<?xml
version="1.0" encoding="UTF-8"
standalone="yes"?><greeting><greeting>Hi
there!</greeting></greeting>
1 * In-bound response
1 * Out-bound request
1 > POST
http://localhost:8080/helloworld-webapp/webresources/myresource
1 > Content-Type:
application/json
1 >
{"greeting":"yo!"}
1 < 200
1 < X-Powered-By: Servlet/2.5
1 < Transfer-Encoding: chunked
1 < Content-Type:
application/json
1 < Server: GlassFish/v3
1 < Date: Tue, 25 Nov 2008 20:19:34 GMT
1 <
{"greeting":"yo!"}
1 * In-bound response
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.191
sec |
The output shows request/response messages when both the tests are run
together. Here are some highlights:
- "GET" and "POST" methods are clearly highlighted.
- The two "Content-Type" headers with value "text/plain"
and "application/json" are output from two tests. The output
from POST method has two Content-Type headers, one for outbound request
and another one for inbound response.
- The body content of POST method is using JSON format.
Jersey and GlassFish
provides a complete server-side and client-side API and framework for
deploying and invoking RESTful Web service endpoints.
How are you using Jersey ?
Send all your questions to
href="mailto:users@jersey.dev.java.net">users@jersey.dev.java.net.
Please leave suggestions on other TOTD (
style="font-weight: bold;">Tip
style="font-weight: bold;">Of
style="font-weight: bold;">The
style="font-weight: bold;">Day) that
you'd like to see.
An archive of all the tips is available
href="http://blogs.sun.com/arungupta/tags/totd">here.
Technorati: totd
glassfish
v3
href="http://technorati.com/tag/embeddable">embeddable
jersey
jsr311
rest
href="http://technorati.com/tag/json">json
href="http://technorati.com/tag/webservices">webservices