spring boot
rest api
base url
configuration
java

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:

java
1@RestController
2@RequestMapping("/api")
3public class MyController {
4
5    @GetMapping("/hello")
6    public String sayHello() {
7        return "Hello, World!";
8    }
9}

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:

java
1@RestController
2@RequestMapping("/api/v1")
3public class MyController {
4
5    @GetMapping("/users")
6    public List<String> getUsers() {
7        return List.of("Alice", "Bob", "Charlie");
8    }
9
10    @PostMapping("/users")
11    public String addUser(@RequestBody String name) {
12        // Add user logic here
13        return "User added: " + name;
14    }
15}

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:

properties
application.base-url=/api/v1

Then use the property in a configuration class:

java
1@Configuration
2public class WebConfig implements WebMvcConfigurer {
3
4    @Value("${application.base-url}")
5    private String baseUrl;
6
7    @Override
8    public void configurePathMatch(PathMatchConfigurer configurer) {
9        // Prefix every @RestController mapping with the configured base URL (Spring 5.1+)
10        configurer.addPathPrefix(baseUrl, HandlerTypePredicate.forAnnotation(RestController.class));
11    }
12}

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.

FeatureDescriptionExample Usage
@RequestMappingSets a base URL at the controller level.@RequestMapping("/api/v1")
Global Base URLApplies a base URL across the applicationDefined in application.properties
Path prefixPrepends 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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design