users@jersey.java.net

Re: [Jersey] Accessing POST body ?

From: Arun Gupta <Arun.Gupta_at_Sun.COM>
Date: Tue, 25 Nov 2008 06:40:54 -0800

Thanks a lot Paul, that worked!

Filed: https://jersey.dev.java.net/issues/show_bug.cgi?id=150

BTW, pushed out a blog showing how to create a simple Jersey app using
Maven and run using embeddable GF at:

http://blogs.sun.com/arungupta/entry/totd_56_simple_restful_web

Couple more coming ...
-Arun

Paul Sandoz wrote:
> Hi Arun,
>
> The client logging will only log at the level of what the Jersey client
> sets to the HttpURLConnection. You can also set logging on the server
> side as well. See here:
>
>
> http://markmail.org/message/p7yxygz4wpakqno5?q=list:net.java.dev.jersey.users+ContainerRequestFilters
>
>
> And see:
>
>
> https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0/api/jersey/com/sun/jersey/api/container/filter/LoggingFilter.html
>
>
> then you can observe what the Content-Type is.
>
> It is currently necessary to set the medias type of the request:
>
> Greeting result =
> createResource().type("application/xml").post(Greeting.class, new
> Greeting("yo!"));
>
> I view this as a bug since a default media type should be derivable from
> the JAXB type. Could you log an issue?
>
> Thanks,
> Paul.
>
> On Nov 25, 2008, at 8:30 AM, Arun Gupta wrote:
>
>> 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
>>
>> ---------------------------------------------------------------------
>> 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