RESTful: how to dynamically extend an API path's (or available resources)?
java, rest, web-services
Solution
You can use the `@Path` annotation on methods inside your Resource class.
@Path("/hello")
public class Hello {
... /* GET/PUT/POST */
@GET
@Path("{id}")
public String myMethod(@PathParam("id") String id) {...}
}
The paths will be concatenated so it will match `/hello/13`. The `{id}` is a placeholder for the actual value entered, which can be retrieved with `@PathParam`. In the previous URI, the String `id` will have the value `13`.
Problem
I've had a nice 'ride' with RESTful technology. I am using a Hello.java resource like this: ``` @Path("/hello") public class Hello { ... /* GET/PUT/POST */ } ``` With this I can access my resource with the path `http://my.host/res/hello` . I want to 'ride' RESTful even harder. Having this one resource path is a bit boring. PROBLEM I would like to have a dynamically created resources like this: - `http://my.host/res/hello` - `http://my.host/res/hello/1` - `http://my.host/res/hello/2` - ... - `http://my.host/res/hello/999` It doesn't make sense to create a .java resource for every `@Path("/hello/1") ... @Path("/hello/999")`. Right? Probably this list of sub-resources could be even bigger or dynamically change in time. What is the solution for that? Thanks.