How to code java jersey REST API with multi-leveled resources?

I want to code a REST API that is multileveled like:


I have a single java class that is a Country resource with @Path("country")

How do I create a and so that my Countryresource can use the StateResource in the way I am planning to use it? Any useful links on how to construct this kind of thing in Java?


The CountryResource class needs to have a method annotated with the @Path to the sub-resource CityResource. By default, you are responsible for creating the instance of CityResource e.g.

class CountryResouce {

    private String stateName;

    public CityResource city(@PathParam("cityName") String cityName) {
        State state = getStateByName(stateName);

        City city = state.getCityByName(cityName);

        return new CityResource(city);


class CityResource {

    private City city;

    public CityResource(City city) { = city;

    public Response get() {
        // Replace with whatever you would normally do to represent this resource
        // using the City object as needed
        return Response.ok().build();

CityResource provides the methods that handle the HTTP verbs (GET in this case).

You should look at the Jersey documentation regarding sub-resource locators for more info.

Also note that Jersey provides a ResourceContext to get it to instantiate the sub-resource. If you're going to use @PathParam or @QueryParam in the sub-resource I believe you need to use this as the runtime doesn't touch sub-resources when created yourself via new.

Need Your Help

Returning a type depending on the parameter

c++ templates c++11 metaprogramming

I want to have such a function that it's return type will be decided within the function(depending on the value of the parameter), but failed implementing it. (template specialization maybe?)


php android mysql

I'm kind of new to SQL, and I'm really only interacting with it enough to get a high score database working. I'm using a php script to access my online MySQL db. Anyways.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.