How to specify prefix for all controllers in Spring Boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Spring Boot applications, managing request mappings effectively is crucial for maintaining clear and organized routes. Prefixing all controllers with a specific path is a common requirement for better versioning and grouping of endpoints. This article explores various methods to set a prefix for all controller routes in a Spring Boot application, delving into technical explanations and examples for effective implementation.
Understanding Spring MVC and Spring Boot
Spring Boot builds on Spring's popular web framework, Spring MVC, to provide an easy-to-integrate web development environment. Controllers in a Spring MVC application handle incoming HTTP requests and provide responses. Typically, you define a controller by annotating a class with @RestController or @Controller, combined with @RequestMapping annotations to specify request paths.
Problem Statement
When developing a Spring Boot application, you might need to add a common prefix to all controller routes. This is useful for versioning (e.g., /api/v1/...) or similar purposes. Implementing this consistently can simplify the configuration and maintenance of routing logic across your application.
Methods to Set a Prefix for All Controllers
1. Using @RequestMapping at Class Level
A straightforward way to set a prefix is to use the @RequestMapping annotation at the class level in each controller. This approach involves directly specifying the prefix when defining each controller.
Pros:
- Simple and explicit
- Easy to understand for each controller
Cons:
- Repetitive across multiple controllers
- Violates the DRY (Don't Repeat Yourself) principle
2. Utilizing a RequestHandler Interceptor
A more elegant solution is to define an interceptor that automatically appends the prefix to each request. Spring's HandlerInterceptorAdapter can be customized to alter request paths before they reach their respective controllers.
Pros:
- Centralized configuration
- Reduces redundancy in each controller
Cons:
- Potential complexity in managing interceptor logic
- Requires careful handling to avoid unexpected route changes
3. Utilizing Properties and Path Patterns
Spring Boot allows externalizing configuration through properties. You can define a prefix in application.properties and reference it in controllers using path variables or directly apply it in the configuration.
Pros:
- Separates configuration from code
- Easy to change the prefix without code modifications
Cons:
- Can become cumbersome for complex configurations
- Requires careful attention to externalized property management
4. Using Global Request Mapping with Custom Configuration Class
You can define a custom component that programmatically configures all controllers with a global prefix. This requires a deeper understanding of Spring Boot's capabilities but can provide a clean solution.
Pros:
- Programmatic control over request patterns
- Offers a clean, uniform configuration
Cons:
- Increased complexity in understanding and maintaining the configuration
- Requires familiarity with Spring's internals
Summary Table
| Method | Pros | Cons |
@RequestMapping at Class Level | Simple and explicit Easy to understand | Repetitive across controllers Violates DRY |
RequestHandler Interceptor | Centralized configuration Reduces redundancy | Complexity in logic Risk of unintended routes |
| Properties and Path Patterns | Configuration separate from code Easy updates | Cumbersome for complex configs Carefully managed |
| Global Request Mapping with Custom Class | Programmatic, clean configuration Uniformity | Complex to maintain Requires Spring expertise |
Conclusion
Adding a prefix to all controllers in a Spring Boot application enhances organization, facilitates API versioning, and aligns URL structures. Several approaches are available, each with unique advantages and implications. Choosing the right method depends on the application's complexity and the development team's familiarity with Spring Boot's capabilities. Understanding these options ensures better management of request mappings and contributes to the overall effectiveness of your Spring Boot applications.
Related reading
- How to specify the JDK version in Android Studio?
- How to split a delimited string to a ListString
- How to split a String by space
- How to start @KafkaListener based on start flag
- How to start new activity on button click
- How to start spring-boot app without depending on Database?
- How to start Spring Boot app in Spock Integration Test
- How to start up spring-boot application via command line?

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.