How to use multiple @RequestMapping annotations in spring?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Spring Framework, the @RequestMapping annotation is used to map web requests to specific handler classes or handler methods. This versatile annotation can be declared at the class or method level in a controller. It is critical in defining which method should be called based on the URL, the HTTP method, or even headers and parameters. Sometimes, however, you might want to handle multiple request mappings in a single controller to group similar operations together, enhance code readability, or simply streamline the configuration of related routes.
Multi-mapping with @RequestMapping
Basic Usage at Class and Method Level
You can use the @RequestMapping annotation at the class level to define a base URI. Then, use it again at the method level to extend this base path:
In this example, listItems() handles requests to /api/items/list, and itemDetail() handles requests to /api/items/detail/{itemId}.
Handling Multiple URLs
The @RequestMapping annotation allows a single method to handle multiple routes by accepting an array of paths:
Here, the homePage() method will handle GET requests for "/", "/home", or "/index".
Combining HTTP Methods and URLs
A more complex scenario might involve handling different types of HTTP methods at the same URL, which can also be achieved by specifying the HTTP method in the @RequestMapping attributes:
This setup routes GET requests to fetch users and POST requests to add a new user, both under the same URL /users.
Benefits and Considerations
Using multiple @RequestMapping annotations can greatly benefit the organization of your controller by clearly segmenting different types of requests and keeping related actions together. However, this also means you must carefully manage route specificity and naming to avoid conflicts and ensure clarity.
Summary Table
The table below summarizes key points related to the use of multiple @RequestMapping annotations:
| Feature | Description | Example |
| Base URI setting | Set base URI at class level, extend by methods. | @RequestMapping("/api/items")
at class level |
| Multiple URLs | Handle multiple URLs in one method. | @RequestMapping({"/", "/home"}) |
| HTTP Methods | Specify different methods for the same URL. | @RequestMapping(value = "/users", method = RequestMethod.GET) |
Further Enhancements and Practices
When utilizing multiple @RequestMapping annotations, consider these best practices to maintain clean and efficient code:
- Use concise paths: Keep URL paths concise and intuitive to understand which controller and method they map to.
- Method naming conventions: Name your handler methods clearly to reflect their purpose and the HTTP action they represent.
- Consistency in method design: To maintain consistency across your application, design all controllers and mappings in a similar style.
In conclusion, the @RequestMapping annotation presents a flexible way to handle web requests in Spring. By using it at both the class and method levels, developers can efficiently organize their web applications, making them easier to maintain and extend. Plus, handling multiple routes with a single annotation can streamline your code and reduce duplication. As with any powerful tool, however, careful planning and consistent application of best practices are essential to harnessing its full potential effectively.
Related reading
- How to use null in switch
- How to use Nullable and Nonnull annotations more effectively?
- How to use OpenAPI oneOf property with openapi-generator-maven-plugin when generating Spring code
- How to use Project Reactor's Scheduler with Executor based libraries?
- How to use projections and specifications with spring data jpa?
- how to use @queuebinding with @rabbitlistener?
- How to use spring boot making a common library
- How to use Spring Boot profiles?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.