users@jersey.java.net

Re: [Jersey] Unable to read request body (JSON) on POST request

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 05 Dec 2008 10:46:07 +0100

Hi Neha,

You are using @PathParam incorrectly. This annotation is utilized to
extract information out from the request URI path component and not
information sent in the POST request body (namely the JSON content).

@QueryParam is used to extract information out from the query
parameters of the request URI query component.

The following overview document helps explain things in more detail:

   http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features

Do this instead:

   @POST
   @Consumes("application/json")
   @Produces("application/json")
   public String setUserStatus(JSONObject in) {
     ...
   }

Paul.

On Dec 4, 2008, at 10:52 PM, Verma, Neha wrote:

>
>
> Hi
> I am sending a POST request in the format below to one of my REST
> services. I want to read the request body (which contains the data in
> JSON format) . What is the best way to read this? Currently, all the
> values in the setUserStatus function are null. I couldn't find any
> example that accepts a JSON format request data. Please help!!
> ************************************************************************
> ********************
>
> Request Header
> Host localhost:8080
> User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
> rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
> Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/
> *;q=0.8
> Accept-Language en-us,en;q=0.5
> Accept-Encoding gzip,deflate
> Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
> Keep-Alive 300
> Connection keep-alive
> X-Client-Id 457607695647864653065542
> X-Seq-Id 1
> Content-Type application/json; charset=UTF-8
> X-Requested-With XMLHttpRequest
> Referer http://localhost:8080/LCAWebtop/rest.html
> Content-Length 59
> Cookie JSESSIONID=B0D3CD38AEC6F8A6E644FF24658BBD6A
> POST
> {"userid":"123","status":"Active","message":"In a meeting"}
>
>
> ************************************************************************
> ********************
>
> My code is as below
>
> Path("/userStatus")
> @Singleton
>
> public class UserStatus {
>
> public JSONArray onlineUsers;
>
> public UserStatus(){
> onlineUsers = new JSONArray();
> JSONObject obj = new JSONObject();
> obj.put("userid","nevermna");
> obj.put("status","Active");
> obj.put("statusmessage","My Messahe");
> onlineUsers.add(obj);
> }
>
> @POST
> @Consumes("application/json")
> @Produces("application/json")
> public String setUserStatus(@Context UriInfo ui,_at_Context Request
> r,_at_PathParam("userid") String userid,_at_PathParam("status") String
> status,
> @PathParam("message") String message) {
> /* PathParms as well as QueryParams is empty here ???? */
> JSONObject obj = new JSONObject();
> obj.put("userid",userid);
> obj.put("status",status);
> obj.put("statusmessage",message);
> onlineUsers.add(obj);
> Log.info(" The user list is "+onlineUsers.toString());
>
> return obj.toString();
> }
> }
>
>
>
> ************************************************************************
> ********************
>
>
>
>
> Thanks!!
> Neha
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - -
>
> This message is intended only for the personal and confidential use
> of the designated recipient(s) named above. If you are not the
> intended recipient of this message you are hereby notified that any
> review, dissemination, distribution or copying of this message is
> strictly prohibited. This communication is for information purposes
> only and should not be regarded as an offer to sell or as a
> solicitation of an offer to buy any financial product, an official
> confirmation of any transaction, or as an official statement of
> Lehman Brothers. Email transmission cannot be guaranteed to be
> secure or error-free. Therefore, we do not represent that this
> information is complete or accurate and it should not be relied upon
> as such. All information is subject to change without notice.
>
> --------
> IRS Circular 230 Disclosure:
> Please be advised that any discussion of U.S. tax matters contained
> within this communication (including any attachments) is not
> intended or written to be used and cannot be used for the purpose of
> (i) avoiding U.S. tax related penalties or (ii) promoting, marketing
> or recommending to another party any transaction or matter addressed
> herein.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>