JAX-RS @Path URI Matching – Static and Regex URIs

Learn to use JAX-RS @Path annotation and write resource path in it for URI matching. Paths can be written in simple form (static or path parameter) or complex form (regex based).

1. JAX-RS @Path URI Matching Types

JAX-RS @Path annotation is used to specify the URI on which the resources will be accessible to other applications or clients. The @javax.ws.rs.Path annotation must exist on either the class and/or a resource method. If it exists on both the class and method, the relative path to the resource method is a concatenation of the class and method.

URIs in @Path are defined in two ways.

  1. Simple URI matching
  2. Regex based URI matching

2. JAX-RS @Path – Simple URI matching

In simplest form, URI consist of static path and if needed some per-defined parameters.

  • @Path ("users") – matches to application/user-management/users.
  • @Path ("users/{id}") – matches to application/user-management/users/12.

3. JAX-RS @Path – Regex based URI matching

In complex form, we can use regular expression based parameter matching.

  • @Path("/users/{id: [0-9]*}") (support digit only) – matches to http://localhost:8080/RESTfulDemoApplication/user-management/users/123456.
  • @Path("/users/{id: [a-zA-Z][a-zA-Z_0-9]}") (first character alphabet and then alpha-numerics) – matches to http://localhost:8080/RESTfulDemoApplication/user-management/users/abc123.

Happy Learning !!

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode