users@jersey.java.net

Re: Question about methods with parameters from _at_UriParam and @ConsumeMime

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 31 Jan 2008 10:19:54 +0100

Hi Dong,

Dong Liu wrote:
> Hi,
>
> Is it possible to have a method that has two parameters like the following?
>
> @Path("{user}/")
> @PUT
> @ConsumeMime("application/json")
> public Response updateAddress(@UriParam("user") String user,
> JSONObject address) throws JSONException{
> ...
> }
>
>

Yes, this is supported. A single non-annotated method parameter is the
Java type of the request entity.


> I tried, but it did not work. It seems that the JSONObject from request
> is not passed.

When you say "not passed" is the 'updateAddress' method called but the
'address' field is null?


> Is there any other way to do this besides creating a new
> resource/class for @Path("{user}/")?
>

Note that the @Path("{user}/") as a '/' at the end so if a the client
request URI does not have a slash then Jersey will send a 307 redirect
response to a URI that has a slash. If you remove the '/' redirection
will not be supported.

I created a very simple root resource:

   import javax.ws.rs.ConsumeMime;
   import javax.ws.rs.PUT;
   import javax.ws.rs.Path;
   import javax.ws.rs.ProduceMime;
   import javax.ws.rs.UriParam;
   import org.codehaus.jettison.json.JSONObject;

   @Path("users")
   public class HelloWorldResource {

     @Path("{user}")
     @PUT
     @ConsumeMime("application/json")
     @ProdiceMime("application/json")
     public String updateAddress(@UriParam("user") String user,
         JSONObject address) {
       System.out.println(user);
       return address.toString();
     }
   }

deployed it then ran a simple curl command:

   curl -v -T a.json http://localhost:8080/Users/users/sdsdsd

where 'a.json' is a file containing the following json:

   {"menu":"abc"}

and curl will PUT this file to the above URL. I got the following output:

> PUT /Users/users/sdsdsd HTTP/1.1
> User-Agent: curl/7.15.5 (i386-pc-solaris2.11) libcurl/7.15.5
                 OpenSSL/0.9.8a zlib/1.2.3 libidn/0.6.8
> Host: localhost:8080
> Accept: */*
> Content-Length: 14
> Expect: 100-continue
>
   < HTTP/1.1 100 Continue
   < HTTP/1.1 200 OK
   < X-Powered-By: Servlet/2.5
   < Server: Sun Java System Application Server 9.1
   < Content-Type: application/json
   < Content-Length: 14
   < Date: Thu, 31 Jan 2008 09:12:22 GMT
   Connection #0 to host localhost left intact
   * Closing connection #0
   {"menu":"abc"}

Hope this helps,
Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109