Scrivio Interview library
InternalsFoundations6 min read · updated 12 November 2026

Two methods in the same resource are annotated @GET @Path("{name}") and @GET @Path("active"). Which one handles GET /users/active, and how does JAX-RS rank candidate paths when several templates match?

GET /users/active is handled by the method with @Path("active"). When several templates match, Jakarta REST sorts the candidates by the number of literal characters (descending), then by the number of template variables (descending), then by the number of variables with their own regular expression (descending), and "active" has 6 literal characters while "{name}" has none. Class-level @Path is matched first, then method-level paths and sub-resource locators, and only then the HTTP method and media types, which is where 405, 415 and 406 come from.

Why this question is asked

Path matching decides which Java method answers a request, so a misunderstanding here produces bugs that look random: the wrong method runs, or an endpoint suddenly returns 405 after someone adds a new method. Interviewers ask this internals question because it tests whether we know the actual algorithm in the specification rather than guessing. In this answer, we will recap how JAX-RS resources declare paths, reproduce the example, walk through the matching algorithm with its sorting rules, see how the HTTP method and media types are chosen after the path, and finish with advice on designing paths that do not depend on subtle ranking.

1A Quick Recap of Resources and Templates

In Jakarta REST (the current name of JAX-RS), a resource class has @Path on the class, and its methods can add their own @Path. A path can contain template variables in braces, such as {name}, which match one path segment and are passed to the method with @PathParam. A variable can also carry its own regular expression, such as {id: \d+}, which limits what it accepts. Without one, the variable matches any characters except a slash, roughly [^/]+.

The runtime, for example Jersey or RESTEasy, turns every template into a regular expression and compares it with the request path. The question is what happens when more than one expression matches.

2Reproducing the Example

Here are the two methods from the question.

Java
@Path("users")
public class UserResource {
@GET @Path("{name}")
public User byName(@PathParam("name") String name) { return users.find(name); }
@GET @Path("active")
public List<User> active() { return users.active(); }
}
// GET /users/active -> active()
// GET /users/ana -> byName("ana")
// GET /users/active/ -> depends on the runtime's trailing-slash handling; check your version

Both templates match the segment active, because {name} matches any segment. The specification resolves the tie by preferring the more specific template, and it defines specific in a precise, countable way.

3How the Runtime Ranks Matching Templates

The specification's matching algorithm works in three stages. First it matches the request path against the @Path of every root resource class and keeps the classes whose template matches the start of the path. Second, inside the chosen class, it matches the remaining part of the path against the method-level @Path values of resource methods and sub-resource locators. Third, among the methods for the final path, it chooses by HTTP method and media type.

Whenever several templates match in the first two stages, they are sorted with three keys. The primary key is the number of literal characters, characters that are not part of a template variable, with more literal characters first. The secondary key is the number of template variables, more first. The tertiary key is the number of variables with a non-default regular expression, more first. The first template in that order wins.

So for /users/active, active has 6 literal characters and {name} has 0, and active wins on the first key without looking any further. The reason for this design is that a literal is a stronger statement of intent than a variable: the developer wrote the exact word.

Sorting keys for the candidate templates
TemplateLiteral charsVariablesRegex variables
active600
{id: \d+}011
{name}010
Sorted from first to last choice: more literal characters win, then more variables, then more regex variables.

4After the Path: Method and Media Types

Once the path is settled, the runtime looks at the methods bound to it. If none of them has the request's HTTP method, for example a DELETE to a path with only @GET, the response is 405 Method Not Allowed. If methods exist but none has an @Consumes that accepts the request's Content-Type, the response is 415 Unsupported Media Type. If none can @Produces a type the client's Accept header allows, the response is 406 Not Acceptable. And if no path matched at all, the response is 404.

Notice the order. The path is chosen before the HTTP method. So if we had @GET @Path("{name}") and @DELETE @Path("active"), a GET /users/active would pick the literal active template first, find only DELETE there, and answer 405, even though the {name} method could have handled a GET. This surprises people, and it is the strongest argument against overlapping templates.

Step throughMatching a request to a method
  1. Match root resource @PathNo match: 404
  2. Match method @Path and locatorsSort by literal chars, variables, regex variables
  3. A method for this HTTP verb?
    YesCheck Content-TypeNo405 Method Not Allowed
  4. @Consumes accepts Content-Type?
    YesCheck AcceptNo415 Unsupported Media Type
  5. @Produces fits Accept?
    YesInvoke the methodNo406 Not Acceptable

5 steps in order. Play them, or step through them one at a time.

5Designing Paths That Do Not Depend on Ranking

The ranking rules are well defined, but code that depends on them is fragile. A reader has to know the algorithm to predict which method runs, and adding a method elsewhere can change the result, as the 405 example shows. A user literally named active also becomes unreachable through /users/{name}.

Clearer designs use distinct literal prefixes or query parameters, for example /users/by-name/{name} for lookup and /users?status=active for filtering, or a constrained template such as {id: \d+} when ids are numeric. Then each request matches exactly one template, and nobody needs to remember the sorting keys.

6Follow-up questions

What is a sub-resource locator and when does it run?

A sub-resource locator is a method with @Path but no HTTP method annotation. It takes part in the second matching stage like a resource method, and when its path matches the start of the remaining URI, the runtime calls it during matching, before choosing any HTTP method. It returns an object, or a class the runtime instantiates, and matching continues on that object with whatever path is left. It is useful for nested resources such as /customers/{id}/orders, where the parent can load and check the customer once.

Does a regular expression on a variable change the ranking?

Only on the third key. A {id: \d+} template and a {name} template both have zero literal characters and one variable, so the regex template ranks higher because it has one non-default regular expression. A literal template still beats both.

Are the rules the same in Jersey and RESTEasy?

Both implement the specification's algorithm, so normal cases behave the same. Edge cases, such as trailing slashes or encoded characters, can differ between implementations and versions, which is another reason to avoid ambiguous designs.

7Conclusion

In this answer, we saw that GET /users/active goes to the @Path("active") method, because Jakarta REST sorts matching templates by literal characters first, then template variables, then variables with regular expressions. The class path is matched first, then method paths and sub-resource locators, and only then the HTTP method and media types, producing 405, 415 or 406 when they do not fit. Because these rules are subtle, distinct literal prefixes make APIs easier to read and safer to change.

What to say in the interviewAbout 80 seconds aloud

Open with

The method with @Path("active") handles GET /users/active. Both templates match, because {name} matches any single segment, so the runtime has to rank them, and the Jakarta REST specification defines exactly how.

Then, if they want more

It sorts the matching templates by the number of literal characters, descending, then by the number of template variables, descending, then by the number of variables that have a non-default regular expression, descending. active has six literal characters and {name} has none, so the literal wins immediately. A template variable without a regex matches anything except a slash.

The overall algorithm has stages: first the class-level @Path of the root resources, then method-level paths and sub-resource locators on the remaining path, and only then the HTTP method and media types.

If no method has the request's verb I get a 405, if no @Consumes matches the Content-Type a 415, and if no @Produces fits the Accept header a 406.

That ordering has a trap: the path is chosen before the verb, so if the literal path only had DELETE, a GET would get 405 even though the template method could serve it. So in practice I would not rely on ranking.

I would use distinct prefixes like /users/by-name/{name}, or a query parameter for filtering, so every request matches one template.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Two methods in the same resource are annotated @GET @Path("{name}") and @GET...": https://scrivio-interview-library.netlify.app/java/jax-rs/8dcf3712e6/