Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS multiple classes with the same path

With JAX-RS, is it possible to have more than one class assigned to a single path? I'm trying to do something like this:

@Path("/foo")
public class GetHandler {
    @GET
    public Response handleGet() { ...
}

@Path("/foo")
public class PostHandler {
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response handlePost() { ...
}

This apparently isn't allowed as I get:

com.sun.jersey.api.container.ContainerException: A root resource, class PostHandler, has a non-unique URI template /foo

I can always create one class to handle requests and then delegate to helper classes. I was hoping there was a standard way of doing so.

like image 318
Steve Kuo Avatar asked Aug 23 '12 17:08

Steve Kuo


2 Answers

The JAX-RS spec doesn't forbid such a mapping. For example, Resteasy JAX-RS implementation allows for it. The feature should be jersey specific.

Regarding:

I can always create one class to handle requests and then delegate to helper classes. I was hoping there was a standard way of doing so.

Usually you have the resource classes with the same name as the path:

@Path("/foo")
public class FooResource {
    @GET
    @Path("/{someFooId}")
    public Response handleGet() {
      ...
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response handlePost() {
       ...
    }
}
like image 184
dcernahoschi Avatar answered Sep 28 '22 19:09

dcernahoschi


You cannot have multiple resources mapped to the same path. I tried that few days back and landed up at similar error.

I ended up doing subpaths such as /api/contacts for one resource and /api/tags for another.

The only other long way is to create resources in multiple packages and then create different app for each.

like image 21
cloudpre Avatar answered Sep 28 '22 20:09

cloudpre