The @Path annotation in JAX-RS (Jakarta RESTful Web Services) maps a URI path to a resource class or a resource method. Placed on a class, it sets the base path for every method in that class. Placed on a method, it appends a sub-path, and it can contain template variables such as {id} that you read with @PathParam.
Without @Path on the class, the runtime does not treat the class as a root resource, so no request is ever routed to it. This article shows how class-level and method-level paths combine, how templates and regular expressions work, and what changed with the move from javax.ws.rs to jakarta.ws.rs.
JAX-RS routes an incoming request in two steps. First it finds the root resource class whose @Path matches the start of the request URI. Then it finds the method inside that class whose @Path (plus HTTP method annotation such as @GET) matches the rest of the URI.
The full URI a client calls is built from several parts:
| Part | Where it comes from | Example |
|---|---|---|
| Context root | The deployment (WAR name or server config) | /shop |
| Application path | @ApplicationPath on your Application subclass | /api |
| Class path | @Path on the resource class | /users |
| Method path | @Path on the resource method | /{id} |
Together they give http://localhost:8080/shop/api/users/42. This is why a class annotated with only @Path("/users") is not reachable at /users when an @ApplicationPath("/api") exists: the application path always comes first.
A class-level @Path makes the class a root resource and defines the base URI for all its methods. Since Jakarta EE 9 the package is jakarta.ws.rs; older Java EE 8 and earlier code uses javax.ws.rs. The annotations themselves are identical.
A GET request to /api/users calls listUsers(). The leading slash in @Path("/users") is optional: the runtime normalises users and /users to the same path, and a trailing slash is also ignored when matching.
A method-level @Path refines the class path. A method without @Path handles the class path itself, as listUsers() does above. A method with @Path handles the class path plus its own value.
Here GET /api/users/42 runs get(42) and DELETE /api/users/42 runs delete(42). The same path can serve several methods because the HTTP method annotation takes part in the selection. UserStore is a placeholder for your own persistence layer.
A path can contain template parameters in curly braces. The runtime captures the matched text and injects it with @PathParam. By default a parameter matches any characters except a slash, which is the same as the regular expression [^/]+.
You can restrict a parameter with a regular expression after a colon:
With these two methods, /api/users/42 goes to byId and /api/users/alice goes to byName. A request such as /api/users/!! matches neither and returns 404. To allow slashes inside one parameter, use a pattern like {path: .+}.
Two further rules decide which method wins when several paths match:
/users/me beats /users/{id}.A method annotated with @Path but with no HTTP method annotation is a sub-resource locator. It returns an object, and JAX-RS continues matching the rest of the URI against that object's annotations. This keeps large APIs split into small classes.
GET /api/orders/7/items calls items(7) and then list() on the returned object. The sub-resource class does not need its own class-level @Path.
/users returns 404 while /api/users works.javax.ws.rs and jakarta.ws.rs imports. On a Jakarta EE 9+ server or Jersey 3.x and later, only jakarta.ws.rs is scanned.@Path on an interface method but not on the implementing class: annotations on an interface are inherited only in the cases the specification allows, so keep them on one side.No. @Path("users") and @Path("/users") are equivalent, and a trailing slash in the request URI is also tolerated. Using the leading slash is a readability convention.
@ApplicationPath goes on an Application subclass and sets the prefix for the whole JAX-RS application. @Path goes on individual resource classes and methods and is appended to that prefix.
Add a parameter annotated with @PathParam("name"), where name is the text inside the braces. JAX-RS converts the string to types such as int, long, UUID or LocalDate when the type has a suitable factory or constructor.
No, @Path is not repeatable. If you need several URIs for the same logic, write small delegating methods or use one template with a regular expression.
@Path is how JAX-RS ties URIs to Java code. Put it on a class to create a root resource, on methods to add sub-paths, use {name} templates with optional regular expressions to capture values, and use sub-resource locators to split large APIs. Remember the final URI also includes the context root and @ApplicationPath, and use the jakarta.ws.rs package on current Jakarta EE servers.