users@jersey.java.net

Re: [Jersey] Accessing POST body ?

From: Arun Gupta <Arun.Gupta_at_Sun.COM>
Date: Mon, 24 Nov 2008 23:30:09 -0800

Thanks Naresh,

I'm still seeing an error.

Here are my methods:

-- resource
     @POST
     @Consumes("application/xml")
     public Greeting postIt(Greeting greeting) {
         System.out.println(greeting.greeting);
         return greeting;
     }

-- test
     private WebResource createResource() {
         Client client = Client.create();
         WebResource resource =
client.resource("http://localhost:8080/helloworld-webapp/webresources/myresource");
         resource.addFilter(new LoggingFilter());
         return resource;
     }

     public void testPost() {
         Greeting result = createResource().post(Greeting.class, new
Greeting("yo!"));
         assertTrue(result.greeting.equals("yo!"));
     }

-- Greeting
@XmlRootElement
public class Greeting {

     public String greeting;

     public Greeting() {
     }

     public Greeting(String greeting) {
         this.greeting = greeting;
     }
}

But still the response is coming back with the following body:

<h1>HTTP Status 415 - </h1><hr/><p><b>type</b> Status
report</p><p><b>message</b></p><p><b>description</b>The server refused
this request because the request entity is in a format not supported by
the requested resource for the requested method
().</p><hr/><h3>GlassFish/v3</h3></body>

Any idea ?

-Arun

Srinivas Naresh Bhimisetty wrote:
> I'm sorry for the duplication. There was some issue with the mail server
> here, so I thought the first mail wasn't sent.
>
> -Naresh
>
> Srinivas Naresh Bhimisetty wrote:
>> Hi Arun,
>>
>> the POSTed data could be accessed in the resource method by
>> annotating it with the @Consumes annotation.
>> The method needs to be annotated with @Consumes(mime-type) and should
>> have a parameter matching the mime-type. Jersey then gets the POSTed
>> data to this method parameter.
>>
>> It would be something like this:
>> ----------------------------------------------------------------------------------------
>>
>> @POST
>> @Consumes("text/plain")
>> public String doSomething(String postedData) {....}
>> -----------------------------------------------------------------------------------------
>>
>>
>> -Naresh
>>
>> Arun Gupta wrote:
>>> POSTing a request using client APIs as:
>>>
>>> ClientResponse cr = resource.
>>> type("text/plain").
>>> post(ClientResponse.class, "some stuff");
>>>
>>> How do I access "some stuff" in a method on my resource ?
>>>
>>> -Arun
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
Application Platform, Sun Microsystems, Inc.
Blog: http://blogs.sun.com/arungupta