December 1, 2008

TOTD #58: Jersey and GlassFish – how to process POST requests ?

Categories: totd, webservices

Lets extend the Jersey endpoint (TOTD# 56) and client (TOTD# 57) such that it can accept a POST request and then invoke it.

  1. Add a new method to “MyResource.java” from TOTD# 56 as:

        @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.

  2. Add a new method to “AppTest.java” from TOTD# 57 as:
        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”.

  3. Running the test as “mvn test” shows the following output:
    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:

    1. “GET” and “POST” methods are clearly highlighted.
    2. 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.
    3. 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 .

Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. An archive of all the tips is available here.

Technorati: totd glassfish v3 embeddable jersey jsr311 rest json webservices

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • E-mail this story to a friend!
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #57: Jersey Client API – simple and easy to use
  2. TOTD #56: Simple RESTful Web service using Jersey and Embeddable GlassFish – Text and JSON output
  3. TOTD #8: Generating JSON using JAXB annotations in Jersey
  4. TOTD #10: Consuming JSON and XML representations generated by a Jersey endpoint in a jMaki Table widget
  5. TOTD #98: Create a Metro JAX-WS Web service using GlassFish Tools Bundle for Eclipse

4 Comments »

  1. [Trackback] Today Sun announces the availability of Java FX 1.0. JavaFX 1.0 is a rich client platform for creating and delivering Rich Internet Applications across all screens (desktop, browser, and mobile) of your life. It consists of the following key…

    Comment by Arun Gupta's Blog — December 4, 2008 @ 7:12 am

  2. Ttt

    Comment by dizi izle — February 21, 2009 @ 9:25 am

  3. tsKler

    Comment by dizi izle — February 21, 2009 @ 9:26 am

  4. thx u aran gupta

    Comment by sinema izle — March 7, 2009 @ 3:36 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress
7997 visits from Sep 11, 2009