Can we have more than one @Path
annotation for same REST method i.e. the method executed is the same, but it is executed on accessing more than one URL?
E.g.: I want to run the searchNames()
method on both http://a/b/c
and http://a/b
.
The @Path annotation is used to specify the URI through which a resource and an API can be accessed. Resource in this case is the REST Web service itself. Thus this annotation is present at the class level as well as the method level. It is mandatory to annotate a REST Web resource class with the @Path annotation.
The @Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource.
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.
Annotations are like meta-tags that you can add to the code and apply to package declarations, type declarations, constructors, methods, fields, parameters, and variables.
You can't have mutliple @Path
annotations on a single method. It causes a "duplicate annotation" syntax error.
However, there's a number of ways you can effectively map two paths to a method.
The @Path
annotation in JAX-RS accepts parameters, whose values can be restricted using regular expressions.
This annotation:
@Path("a/{parameter: path1|path2}")
would enable the method to be reached by requests for both /a/path1
and /a/path2
. If you need to work with subpaths, escape slashes: {a:path1\\/subPath1|path2\\/subPath2}
Alternatively, you could set up a redirection. Here's a way to do it in Jersey (the reference implementation of JAX-RS), by defining another subresource. This is just an example, if you prefer a different way of handling redirections, feel free to use it.
@Path("basepath") public class YourBaseResource { //this gets injected after the class is instantiated by Jersey @Context UriInfo uriInfo; @Path("a/b") @GET public Responce method1(){ return Response.ok("blah blah").build(); } @Path("a/b/c") @GET public Response method2(){ UriBuilder addressBuilder = uriInfo.getBaseUriBuilder(); addressBuilder.path("a/b"); return Response.seeOther(addressBuilder.build()).build(); } }
If you're going to need such functionality often, I suggest intercepting the incoming requests using a servlet filter and rewriting the paths on the fly. This should help you keep all redirections in one place. Ideally, you could use a ready library. UrlRewriteFilter
can do the trick, as long as you're fine with a BSD license (check out their google code site for details)
Another option is to handle this with a proxy set up in front of your Java app. You can set up an Apache server to offer basic caching and rewrite rules without complicating your Java code.
As explained in Tom's answer, you can not use more than one @Path
annotation on a single method, because you will run into error: duplicate annotation
at compile time.
I think the simplest way to get around this is to use method overloading:
@Path("{foo}") public Response rest(@PathParam("foo") final String foo) { return this.rest(foo, ""); } @Path("{foo}/{bar}") public Response rest(@PathParam("foo") final String foo, @PathParam("bar") final String bar) { return Response.ok(foo + " " + bar).build(); }
You could also use more different method names if you run into the case where multiple overloaded methods have the signature.
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