I have been writing a JAX-RS based ReST application using Apache Wink and I understood the concept of association between path params to resource handle class. Here I see that, We can define paths using @Path
annotation and corresponding resource which will get called based on HTTP Method..
Now I am looking at something like a resource which should get called for variable number of path parameters.
For example
I want my single resource class CollegeResource
should be called for URIs like /rest/college, /rest/college/subject, /rest/college/subject/teachers,
and it can go up to any number of path parameters.
If I know the number of path params in prior then I could have achieved this using something like this /rest/college/{param1}/{param2}
. But number of path params is unknown. So I felt (I may be wrong) can not use this approach.
One more way I could still use is using query parameters. But I want this to be available to be as path params only.
Is there any way to get this done using apache wink with any other configuration ? If not in Apache wink, any other JAX-RS implementaions support this ?
Path Parameter Example Path parameters are part of the endpoint and are required. For example, `/users/{id}`, `{id}` is the path parameter of the endpoint `/users`- it is pointing to a specific user's record. An endpoint can have multiple path parameters, like in the example `/organizations/{orgId}/members/{memberId}`.
First, it will use pathParam() method to set values for path parameters, and then the remaining path parameters will be considered based on the index. In the above example, “resourcePath” will be set by pathparam() method, and “bookingId” will be set by inline parameter value i.e. 10.
The @PathParam annotation is a type of parameter that you can extract for use in your resource class. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.
You can use a regex, like @Path("/college/{param: .*}")
, then use List<PathSegment>
as a method parameter. For example
@GET
@Path("/college/{params: .*}")
public Response get(@PathParam("params") List<PathSegment> params) {
StringBuilder builder = new StringBuilder();
for (PathSegment seg: params) {
builder.append(seg.getPath());
}
return Response.ok(builder.toString()).build();
}
C:\>curl -v http://localhost:8080/college/blah/hello/world/cool
Result:blahhelloworldcool
But personally, I would stay away from this kind of thing. Your URI paths (templates) should have some semantic meaning. Allowing an arbitrary number of path params, that may not have any meaning, is error prone, and IMO, is a cause for redesign. I would need to know the semantics behind this design choice before I can offer any advice though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With