users@jersey.java.net

Re: [Jersey] Validate number of Path segments

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 20 Aug 2009 10:55:21 +0200

Hi,

IMHO 404 is the right status code to return to the client, rather than
400. A 404 correctly tells the client the resource is not found for
the request URI. A 400 would mean something else, like the resource
exists but the client is in error.

Usually you do not need to signal such explicit errors because the
client is informed by the server of URLs in the representations
returned in responses, and often Human readable documentation informs
the developer of the URI patterns. I have seen cases where Web servers
return a 404 and list a set of possible similar URIs to resources.

If you want a "catch all" method you can do:

   @GET
   @Path("test/{item1}/{item2}")
   public String getFileState(
           @PathParam("item1") String item1,
           @PathParam("item2") String item2)
   {
       return "**" + item1 + "**" + item2 + "**";
   }

   @GET
   @Path("test/{x: .+}") // match one or more characters
   public void notFound()
   {
     throw new WebApplicationException(404);
   }

The @Path for "getFileState" has priority over the @Path for "notFound".

Paul.



On Aug 19, 2009, at 6:17 PM, dloy wrote:

> Another beginner question. Is there any mechanism in Jersey for
> trapping the condition that too many or too few
> path segments exist, with out creating a template for each possible
> error (e.g. REGEX).
>
> In this example, testIT requires 2 and only 2 segments following .../
> test/
> @GET
> @Path("test/{item1}/{item2}")
> public String getFileState(
> @PathParam("item1") String item1,
> @PathParam("item2") String item2)
> {
> return "**" + item1 + "**" + item2 + "**";
> }
>
> One approach is to include a @Path for every exception.
>
> @GET
> @Path("test/{item1}")
> public String getFileState()
> {
> throw new WebApplicationException(400);
> }
>
> @GET
> @Path("test/{item1}/{item2}/{item3}")
> public String getFileState()
> {
> throw new WebApplicationException(400);
> }
>
> @GET
> @Path("test/{item1}/{item2}/{item3}/{item4}")
> public String getFileState()
> {
> throw new WebApplicationException(400);
> }
>
> Is there some wild card mechanism on @Path to handle the too many
> condition? Is there some general mechanism
> to specify that only 2 path elements are allowed following test/?
>
> Thanks
> David
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>