@javax.ws.rs.Path
annotation’s value is a URI path template relative to base URI. Its value defines the URI matching pattern for incoming HTTP requests. The default value of the template parameter matches any text passed in but the template parameters can optionally specify a regular expression used to match their value and are not just limited to simple wildcard matching express.
Dependencies
See @Path annotation for details.
@Path with regular expression to match any digit
One can specify a template parameter within curly braces, for example @Path("{someParam}")
. Here somParam
is the template parameter and its value can be accessed using @PathParam
annotation.
In the below example, @Path("{id: \\d+}")
, contains a regular expression to match only digits.
RegularExpressionExamples:
package com.javarticles.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("regexExamples") @Produces(MediaType.TEXT_PLAIN) public class RegularExpressionExamples { @Path("{id: \\d+}") @GET public String findUserById(@PathParam("id") int userId) { return "Find users by id <" + userId + ">"; } }
Enter http://localhost:8280/myapp/regexExamples/123
in browser.
Output:
Find users by id
@Path with regular expression to match any String
If we want to find the user based on user’s name rather than ID, then we need to use a regular expression that can allow any character from a through z or A through Z, inclusive. For example, @Path("{name: [a-zA-Z]+}")
.
If we we want to include numeric as well then @Path("{name: [a-zA-Z0-9]+}")
RegularExpressionExamples:
package com.javarticles.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("regexExamples") @Produces(MediaType.TEXT_PLAIN) public class RegularExpressionExamples { @Path("{id: \\d+}") @GET public String findUserById(@PathParam("id") int userId) { return "Find users by id <" + userId + ">"; } @Path("{name: [a-zA-Z]+}") @GET public String findUserByName(@PathParam("name") String userName) { return "Find users by name <" + userName + ">"; } @Path("{name: [a-zA-Z0-9]+}") @GET public String findUserByNameIdMix(@PathParam("name") String userName) { return "Find users by name (mixed) <" + userName + ">"; } }
Enter http://localhost:8280/myapp/regexExamples/John
.
Output:
Find users by name
Enter http://localhost:8280/myapp/regexExamples/John9
.
Output:
Find users by name (mixed)
@Path with multiple template expressions
Regular expressions are not limited in matching one segment of a URI. These template parameters can be embedded anywhere within an @Path declaration.
For example, @Path("{a: \\d+}-{b: \\d+}-{c: \\d+}")
contains three segments.
RegularExpressionExamples:
package com.javarticles.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("regexExamples") @Produces(MediaType.TEXT_PLAIN) public class RegularExpressionExamples { @Path("{id: \\d+}") @GET public String findUserById(@PathParam("id") int userId) { return "Find users by id <" + userId + ">"; } @Path("{name: [a-zA-Z]+}") @GET public String findUserByName(@PathParam("name") String userName) { return "Find users by name <" + userName + ">"; } @Path("{name: [a-zA-Z0-9]+}") @GET public String findUserByNameIdMix(@PathParam("name") String userName) { return "Find users by name (mixed) <" + userName + ">"; } @Path("{a: \\d+}-{b: \\d+}-{c: \\d+}") @GET public String findUserByGroupId(@PathParam("a") int a, @PathParam("b") int b, @PathParam("c") int c) { return "Collective ID is " + a + "-" + b + "-" + c; } }
Enter http://localhost:8280/myapp/regexExamples/123-456-789
in browser.
Output:
Collective ID is 123-456-789
Download the source code
This was an example about using JAX-RS regular expressions in @Path
.