users@jersey.java.net

Re: [Jersey] Can I write a webservice that recieves a POST request without any parameters?

From: Craig McClanahan <craigmcc_at_gmail.com>
Date: Mon, 4 Oct 2010 22:10:18 -0700

On Mon, Oct 4, 2010 at 9:43 PM, Jan Algermissen <algermissen1971_at_mac.com>wrote:

> Use DELETE.....
>
>
Oh yuck :-).

If the transaction being triggered is idempotent (that is, there will be no
side effects if the same request gets submitted more than once), you could
possibly argue that this is appropriate -- the HTTP spec requires a DELETE
implementation to be idempotent. And that would likely be the case for this
particular scenario (clear out an in-memory value) -- but it's potentially
starting down a slippery slope. Non-idempotent transactions seem to be much
more common in my experience, and POST is the right answer there.

At any rate, my previous reply included a way I was able to convince Jersey
to accept a POST with no actual message body.


> Jan
>

Craig


>
> On 04.10.2010, at 19:22, panama joe <athomewithagroovebox_at_gmail.com>
> wrote:
>
> >
> > So, basically, I'm wanting to trigger an event on my server that doesn't
> > require any parameters. Specifically, I'm wanting to clear out a value
> > that's being held in memory. Now, WebResource exposes a parameter-less
> POST
> > method, so I can write a JUnit test that looks like this :
> >
> > try {
> > URI baseUri =
> > UriBuilder.fromUri("http://localhost:8081/myWebApp/rest/Semaphore/Clear
> ").build();
> > WebResource service = client.resource(baseUri);
> > service.post();
> > System.out.println("Semaphore cleared");
> > }
> > catch (Exception ex) {
> > fail(ex.toString());
> > }
> >
> > So I would think that I should be allowed to write webservice code that
> > looks like this :
> >
> > @Path("/Semaphore")
> > public class Semaphore {
> > static private Result lastResult = SemaphoreConstants.NULL_RESULT;
> > static private Status status = Status.STABLE;
> > static private Object RESULT_LOCK = new Object();
> >
> > @POST
> > @Path("Clear")
> > static public void clear() {
> > synchronized(RESULT_LOCK) {
> > status = Status.STABLE;
> > lastResult = SemaphoreConstants.NULL_RESULT;
> > }
> > }
> >
> > ...
> >
> > }
> >
> > However, when I hit that webservice with my JUnit test, I get back :
> > junit.framework.AssertionFailedError:
> > com.sun.jersey.api.client.UniformInterfaceException: POST
> > http://localhost:8081/myWebApp/rest/Semaphore/Clear returned a response
> > status of 400
> >
> > I know that I could just implement this function as a GET request, but
> that
> > seems semantically wrong. Likewise, I know that I could implement it as
> a
> > more traditional POST request where I'm actually providing the
> NULL_RESULT
> > values as parameters, but that seems cumbersome and unnecessary.
> >
> > Is there a way to successfully receive a POST request without parameters?
> > Or do I just need to suck it up and implement this differently?
> > --
> > View this message in context:
> http://jersey.576304.n2.nabble.com/Can-I-write-a-webservice-that-recieves-a-POST-request-without-any-parameters-tp5600043p5600043.html
> > Sent from the Jersey mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > 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
>
>