问题描述:

New to Jersey(REST Framework for Java) and I'm trying to setup two resources, in two separate classes that share a root path, and I'm having a problem.

So, I have something like:

@Path("/users")

public class User extends RestSupport {

@GET

@Path("/{user_uuid}")

public String get(@PathParam("user_uuid") String uuid) {

return "Hello User " + uuid;

}

}

The above class works. However, I want to create a child resource in a separate class. But when I do this, it seems to create a URI naming conflict. So, here, I want to get all the pets for a particular users

@Path("/users")

public class Pets extends RestSupport {

@GET

@Path("/{user_uuid}/pets")

public String get(@PathParam("user_uuid") String uuid) {

return "Hello Pets " + uuid;

}

}

These top-level resources have lots of child resources, so I'm looking for the best way to organize them. Any help would be appreciated.

网友答案:

Change the path of Pets class from
@Path("/users")
to
@Path("/users/{user_uuid}/pets")

网友答案:

Don't add the HTTP annotation @GET on your Users root resource method if you want Jersey to delegate calls to a child resource. Consider a User class:

public class User {

  String uuid;
  User(String id) { this.uuid = id; }

  @GET
  public String get() { return "Hello user " + uuid; }

  @GET
  @Path("/pets")
  public String getPets() { return "Hello pets " + uuid; }

}

and then adjust your Users resource:

@Path("/users")
public class Users  {

  @Path("/{user_uuid}")
  public User get(@PathParam("user_uuid") String uuid) {
    // Get the user from the DAO here...
    return new User(uuid);
  }

}
相关阅读:
Top