views:

47

answers:

1

Hi all,

I've used a regular expression in @Path to achieve overloading and at first I thought it was really neat, but overloading methods is usually not good practice. Does the same apply to RESTful web services? Is there a better way to achieve this using JAX-RS?

So I can now call my getProject REST service by /project/ProjectNumber1000 or /project/12345

@Path("/project")
public class ProjectPropertiesResource
{
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{name : [a-zA-Z]+}")
    public Response getProjectPropertiesByName(@PathParam("name") String name)
    {
         ...
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{id : \\d+}")
    public Response getProjectPropertiesById(@PathParam("id") long id)
    {
         ...
    }
}
+2  A: 

You can do it, however, only one of the overloads should actually return response body with a 200. The other overloads should return a 303 redirect to the URI that returns the body.

This will ensure that caches only have one copy of the resource and if you do PUT or POST on the main URI you will invalidate the one copy. Otherwise, you can start to get inconsistent results due to different versions existing in the cache.

Darrel Miller
OK that is a good point, so I would just use Response.temporaryRedirect in one of the methods
willcodejavaforfood
Yes, something like that. 303 is actually "See Other" not "temporary redirect".
Darrel Miller
OK the 'Used in the redirect-after-POST (aka POST/redirect/GET) pattern.' comment in the JavaDoc scared me off that one
willcodejavaforfood