How to Change Context Path in Spring Boot
In a Spring Boot application, the context path is the URL prefix that is used to access the application’s resources. By default, the context path is set to /
, which means the application is accessible at the root URL. However, there are several ways to change the context path to suit your needs. In this tutorial, we will explore different methods to change the context path in a Spring Boot application.
Prerequisites
- JDK 17 or later
- Maven or Gradle
- IDE (IntelliJ IDEA, Eclipse, etc.)
Step 1: Create a Spring Boot Project
Use Spring Initializr to create a new project with the following dependencies:
- Spring Web
Download and unzip the project, then open it in your IDE.
Step 2: Change the Context Path Using application.properties
The most straightforward way to change the context path is by modifying the application.properties
file. This file is located in the src/main/resources
directory.
# src/main/resources/application.properties
server.servlet.context-path=/api
Explanation:
server.servlet.context-path=/api
: Sets the context path to/api
. You can change this to any desired path.
Step 3: Change the Context Path Using application.yml
If you prefer using YAML for configuration, you can change the context path by modifying the application.yml
file. Create this file in the src/main/resources
directory if it doesn't exist.
# src/main/resources/application.yml
server:
servlet:
context-path: /api
Explanation:
server.servlet.context-path: /api
: Sets the context path to/api
. You can change this to any desired path.
Step 4: Change the Context Path Programmatically
You can also change the context path programmatically by creating a WebServerFactoryCustomizer
bean. This approach is useful if you want to dynamically set the context path based on some condition or environment variable.
package com.example.demo;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/api");
}
}
Explanation:
@Configuration
: Marks the class as a source of bean definitions.WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
: Customizes the web server factory to change the context path.factory.setContextPath("/api")
: Sets the context path to/api
. You can change this to any desired path.
Step 5: Change the Context Path Using Command-Line Arguments
You can override the context path by passing a command-line argument when running the application. This method is useful for temporary changes or for running the application with different context paths in different environments without changing the code or configuration files.
./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.servlet.context-path=/api
Explanation:
-Dspring-boot.run.arguments=--server.servlet.context-path=/api
: Passes the--server.servlet.context-path=/api
argument to the Spring Boot application, overriding the default context path. You can change this to any desired path.
Step 6: Change the Context Path Using Environment Variables
You can use environment variables to change the context path. This approach is useful for containerized applications or environments where you can’t easily modify configuration files or command-line arguments.
export SERVER_SERVLET_CONTEXT_PATH=/api
./mvnw spring-boot:run
Explanation:
export SERVER_SERVLET_CONTEXT_PATH=/api
: Sets theSERVER_SERVLET_CONTEXT_PATH
environment variable to/api
../mvnw spring-boot:run
: Runs the Spring Boot application, which will use theSERVER_SERVLET_CONTEXT_PATH
environment variable to set the context path. You can change this to any desired path.
Step 7: Create a Simple REST Controller for Testing
To verify that the context path has been changed, let’s create a simple REST controller.
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Explanation:
@RestController
: Marks the class as a REST controller.@GetMapping("/hello")
: Maps GET requests to/hello
to thehello
method.hello()
: Returns a simple greeting.
Step 8: Running and Testing the Application
8.1 Run the Application
Run the Spring Boot application using any of the methods mentioned above.
8.2 Test the Application
Open your browser and navigate to http://localhost:8080/api/hello
(assuming you set the context path to /api
). You should see the following output:
Hello, World!
Conclusion
In this tutorial, you have learned various methods to change the context path in a Spring Boot application. These methods include modifying application.properties
or application.yml
, using a WebServerFactoryCustomizer
bean, passing command-line arguments, and setting environment variables. This flexibility allows you to configure your application to use different context paths in different environments easily.
Original Post:
https://www.sourcecodeexamples.net/2024/05/how-to-change-context-path-in-spring-boot.html