@RequestMapping at Class vs Method Level — URL Composition
Fast recall
Quick revision
Core answer
Practical understanding of Spring MVC URL structuring and how to organize REST endpoints cleanly using hierarchical path composition.
Repeating the full base path in every method mapping instead of extracting it to class level, leading to duplication and harder maintenance.
Mention that @RequestMapping can map multiple paths in an array and that class-level mappings can also specify common headers, params, or produces/consumes constraints.
Interview-ready guide
Interview answer
When you put @RequestMapping at the class level, it sets a base URL prefix for all methods in that controller.
Then your method-level annotations like @GetMapping or @PostMapping add sub-paths to that base. Spring concatenates them together to build the full endpoint.
For example, if you have @RequestMapping with "/api/users" on the class, and @GetMapping with "/{id}" on a method, Spring combines them into /api/users/{id}. This hierarchical approach eliminates duplication. Instead of writing /api/users twenty times in every method, you write it once at class level.
It makes refactoring way easier—if you need to change the base path to /v2/users, you edit one line instead of hunting through every method. A cool detail is that class-level @RequestMapping can also set common constraints like produces equals "application/json" or specific headers, and all methods inherit those settings unless they override them.
A common mistake is repeating the full base path in every method mapping. That violates the DRY principle and creates maintenance headaches. Another gotcha is using @RequestMapping at method level instead of @GetMapping.
That removes the HTTP method restriction, so your endpoint accidentally accepts all methods unless you specify method equals RequestMethod dot GET explicitly. Always use the specific annotations at method level.
@RequestMapping at Class vs Method Level — URL Composition
Direct Answer
Class-level @RequestMapping sets a base URL prefix that applies to all methods in the controller. Method-level annotations (@GetMapping, @PostMapping, etc.) add sub-paths to this base. Spring concatenates them: a class-level /api/users plus a method-level /{id} creates the full endpoint /api/users/{id}, avoiding path duplication.
Code Example
@RestController
@RequestMapping("/api/users") // Base path for all methods
public class UserController {
@GetMapping // Endpoint: GET /api/users
public List<User> getAllUsers() {
return List.of(new User(1L, "Alice"), new User(2L, "Bob"));
}
@GetMapping("/{id}") // Endpoint: GET /api/users/{id} (base + sub-path)
public User getUserById(@PathVariable Long id) {
return new User(id, "Alice");
}
@PostMapping // Endpoint: POST /api/users
public User createUser(@RequestBody User user) {
return user;
}
@GetMapping({"/search", "/find"}) // Multiple paths: GET /api/users/search OR /api/users/find
public List<User> searchUsers(@RequestParam String name) {
return List.of(new User(1L, name));
}
}
record User(Long id, String name) {}
Common Mistakes & Pitfalls
- Repeating Base Paths: Learners write @GetMapping("/api/users/{id}") on every method instead of extracting /api/users to class level, violating DRY and making refactoring painful when paths change.
- Forgetting Slash Rules: Mixing /api/users (trailing slash absent) at class level with /{id} (leading slash present) at method level works, but inconsistency causes confusion. Spring normalizes slashes, but be consistent.
- Overriding Accidentally: Adding @RequestMapping at method level instead of @GetMapping completely replaces the HTTP method constraint, so your endpoint accepts ALL methods (GET, POST, DELETE) unless you specify method = RequestMethod.GET. Always use the specific annotations like @GetMapping at method level.
Check your understanding
Answer these without looking back at the article.
- How do you implement API versioning with @RequestMapping?
- What happens if both class-level and method-level @RequestMapping define the same path?
- Can you map multiple URL paths to the same controller class?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.