How to set a base URL for REST controllers in Spring Boot
Codemia Knowledge Hub ·
Put @RequestMapping on the controller class: @RequestMapping("/api/v1") on a @RestController prefixes every endpoint in that class with /api/v1. To manage the prefix in one place, store it in application.properties and read it in your web configuration.
A base URL keeps URL patterns organized and makes API versioning easier, since you can change the whole prefix at once.
How controller mappings combine
A class annotated with @RestController handles HTTP requests, and @RequestMapping, @GetMapping, @PostMapping and similar annotations map requests to its methods. A class-level mapping is combined with each method-level mapping:
Here MyController handles GET requests on /api/hello.
Setting the prefix with class-level @RequestMapping
This is the main way to set a base URL. Every method in the controller inherits the prefix:
All endpoints in MyController are served under /api/v1, so these are GET /api/v1/users and POST /api/v1/users.
Reading a global base URL from properties
To apply a base URL application-wide, define it in application.properties or application.yml:
Then use the property in a configuration class:
addPathPrefix reads the value from the properties file and prepends it to the mappings of every class annotated with @RestController, so a controller mapped to /users is served at /api/v1/users. Other handlers, such as static resources, keep their paths. For a prefix on the whole application, including static content, Spring Boot's server.servlet.context-path property is the simpler option.
| Feature | Description | Example Usage |
@RequestMapping | Sets a base URL at the controller level. | @RequestMapping("/api/v1") |
| Global Base URL | Applies a base URL across the application | Defined in application.properties |
| Path prefix | Prepends the base URL to all @RestController mappings. | configurer.addPathPrefix(...) |
Versioning APIs through the prefix
Putting a version in the base URL (for example /api/v1) lets you introduce /api/v2 later while existing clients keep using /api/v1.
Base URLs decided at runtime
Sometimes the base URL depends on conditions or configuration fetched at runtime. In that case you might take it from a settings microservice or from feature flags that decide the current URL scheme.
Keeping prefixes consistent across services
A consistent base URL structure across microservices or modules lets consumers (other services or client apps) predict endpoint patterns. Keeping the prefix in properties files also keeps the code clean.
Related reading
- How to set connection timeout with OkHttp
- How to set HTTP status code from ASP.NET MVC 3?
- How to set HttpResponse timeout for Android in Java
- How to set multiple headers at once in Spring WebClient?
- How to set compileJava' task ( 11) and 'compileKotlin' task (1.8) jvm target compatibility to the same Java version in build.gradle.kts?
- How to set delay in android?
- How to set proxy on spring oauth2 OAuth2AccessToken request or How to override OAuth2AccessTokenSupport restTemplate variable?
- How to set rate limit for each user in Spring Boot?
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.