Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does Spring choose between two mappings, GET /users/{id} and GET /users/me, for a request to /users/me?

Spring picks the most specific mapping that matches the request, and a literal path is always more specific than a pattern; in fact RequestMappingHandlerMapping checks literal paths first and only falls back to patterns when none fits. Among patterns, comparison starts with the path: with PathPattern, a pattern with fewer URI variables and wildcards is more specific, so /users/{id} loses to the literal /users/me, and the request goes to the me method. Other conditions such as params, headers, consumes and produces also take part in the comparison, and if two matches are equally specific, the request fails with an ambiguous handler methods error.

Why this question is asked

Endpoints like /users/me next to /users/{id} are very common, and they work so naturally that many developers never ask why. The interesting part appears when they do not work: two variables at the same position, or a regex that excludes a value. Interviewers ask this because it tests whether we know how RequestMappingHandlerMapping decides, not just that it does. In this answer, we will set up the two mappings, see how Spring finds the candidates, how the most specific one is chosen, how other conditions take part, and what an ambiguous match looks like.

1Setting Up the Two Mappings

Here are the two endpoints. A request for /users/me could match both, because {id} is a URI variable, a placeholder that matches any single path segment, and me is a valid segment.

Java
@RestController
@RequestMapping("/users")
class UserController {
@GetMapping("/{id}")
UserDto byId(@PathVariable String id) { return service.find(id); }
@GetMapping("/me")
UserDto me(Principal principal) { return service.findByLogin(principal.getName()); }
}
// GET /users/me -> me() the literal wins
// GET /users/42 -> byId("42") only the pattern matches

Notice that the order of the methods in the class does not matter. Spring does not take the first match in declaration order; it looks for the most specific one.

2Finding the Candidates

At startup, RequestMappingHandlerMapping builds a RequestMappingInfo for each method: its path patterns, HTTP methods, params, headers, consumes and produces conditions. It also keeps an index of direct paths, the mappings whose pattern is a plain literal with no variables, such as /users/me.

For a request, it looks up the path in that index first and checks the conditions of those mappings; a mapping whose method is POST, or whose consumes does not fit the request's content type, drops out. Only if no direct match survives does it check every pattern mapping. So for GET /users/me the literal mapping is found straight away and /users/{id} is not even tried, while for GET /users/42 there is no direct path, all patterns are checked, and /users/{id} is the one that matches. This shortcut is an optimization; it gives the same answer the specificity rule below would give, because a literal is always more specific than a pattern.

3Choosing the Most Specific Match

When several pattern mappings match, for example /users/{id} and /users/*, the candidates are sorted with a comparator that asks, condition by condition, which mapping is more specific for this request. Path patterns are compared first. Spring 6 uses PathPattern, the parsed pattern type, and its specificity rule counts URI variables and wildcards: the pattern with fewer of them is more specific. By that rule /users/me, with none, beats /users/{id}, with one. Wildcards make a pattern more general still, and a ** catch-all is the most general of all.

Only if the patterns are equally specific does the comparator move on to the next conditions: params, headers, consumes, produces and HTTP methods, where a mapping with a more specific condition wins.

Step throughHow GET /users/me is resolved
  1. Look up the direct path index/users/me is a literal mapping
  2. Direct match whose conditions fit?
    YesUse direct matches: me()NoCheck all pattern mappings
  3. Sort candidates by specificitypatterns first, then params, headers, consumes, produces
  4. Top two equally specific?
    YesIllegalStateException: Ambiguous handler methodsNoUse the best match

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

4How Other Conditions Take Part

Conditions make two mappings with the same path distinct. Here two methods share /users/{id} but produce different media types. The request's Accept header decides which one matches, and if both could, the more specific produces wins.

Java
@GetMapping(path = "/{id}", produces = "application/json")
UserDto json(@PathVariable String id) { ... }
@GetMapping(path = "/{id}", produces = "text/csv")
String csv(@PathVariable String id) { ... }
// GET /users/42, Accept: text/csv -> csv("42")
// GET /users/42, Accept: application/json -> json("42")
// GET /users/42, Accept: application/xml -> 406 Not Acceptable
@GetMapping(path = "/{id}", params = "view=summary")
UserSummary summary(@PathVariable String id) { ... }
// GET /users/42?view=summary -> summary("42"); the params condition makes it more specific than json()

So specificity is not only about the path. It is the combination of all conditions, with the path compared first.

5When Two Matches Tie

If two mappings are equally specific on every condition, Spring refuses to guess. A common way to get there is two variables at the same position with different names. Because the pattern strings differ, startup accepts them, but at request time both match with the same specificity.

Java
@GetMapping("/{id}")
UserDto byId(@PathVariable String id) { ... }
@GetMapping("/{login}")
UserDto byLogin(@PathVariable String login) { ... }
// GET /users/42 ->
// java.lang.IllegalStateException: Ambiguous handler methods mapped for '/users/42':
// {UserController#byId(String), UserController#byLogin(String)}
// the client sees a 500

The fix is to make the mappings genuinely different, either with distinct paths such as /users/by-login/{login}, or with a regex constraint such as /{id:\d+}, which only matches digits so that byLogin is the only candidate for alphabetic values. Constraints like this also protect /users/me: if someone later removes the me method, /users/me falls through to byId with id = me unless the pattern rejects it.

6Follow-up questions

Does the order of methods or controllers decide the winner?

No. RequestMappingHandlerMapping always compares all matching candidates and sorts them by specificity, so declaration order has no effect. That is deliberate: order-dependent routing breaks silently when someone moves a method or adds a controller in another package.

What does a regex constraint do to specificity?

Its main effect is on matching rather than ranking: /users/{id:\d+} no longer matches /users/me at all, so there is nothing to compare. That makes the routing robust even if the literal endpoint is removed or renamed.

Is this the same with the old AntPathMatcher?

The idea is the same, literal beats variable beats wildcard, but the exact scoring differs slightly. Spring 6 uses PathPattern by default.

7Conclusion

In this answer, we saw that Spring chooses the most specific mapping for /users/me, checking literal paths first as a shortcut and sorting pattern candidates by specificity. Patterns are compared first, and with PathPattern a literal path with no variables beats /users/{id}, so me() is chosen regardless of declaration order. Params, headers, consumes and produces conditions break ties between equal paths, and when two candidates are equally specific on everything, the request fails with an ambiguous handler methods error, which we avoid with distinct paths or regex constraints.

What to say in the interviewAbout 90 seconds aloud

Open with

Spring does not take the first mapping that matches; RequestMappingHandlerMapping chooses the most specific one. For GET /users/me, both /users/me and /users/{id} could match, because a URI variable matches any single segment.

Then, if they want more

As an optimization it checks literal paths first, so /users/me is found directly and the pattern is only tried when no literal fits, but the rule behind it is specificity. Candidates are compared condition by condition, starting with the path pattern.

With PathPattern, which Spring 6 uses by default, a pattern with fewer URI variables and wildcards is more specific, so the literal /users/me, with zero variables, beats /users/{id}, with one, and the me method handles the request. The order in which methods are declared doesn't matter.

If the patterns tie, the other conditions take part: params, headers, consumes and produces, and a mapping with a more specific condition wins, which is how two methods on the same path can be split by Accept header or by a query parameter.

If two candidates are still equally specific, Spring refuses to guess and throws an IllegalStateException saying ambiguous handler methods mapped for that path, which the client sees as a 500. A typical cause is /users/{id} and /users/{login} in the same controller.

I would fix that with distinct paths or regex constraints like digits only for the id, which also keeps /users/me from ever reaching the id method.

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.
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 "How does Spring choose between two mappings, GET /users/{id} and GET /users/me, for a...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/c04508e607/