Spring Boot- How to change default context path
In Spring Boot, you can change the default context path by modifying the application.properties
or application.yml
file. By default '/'(root)
is the default context path in Spring Boot application.
2024-03-03T11:56:35.907+05:30 : Starting Servlet engine: [Apache Tomcat/10.1.18]
2024-03-03T11:56:35.973+05:30 : Initializing Spring embedded WebApplicationContext
2024-03-03T11:56:35.975+05:30 : Root WebApplicationContext: initialization completed in 1897 ms
2024-03-03T11:56:36.488+05:30 : LiveReload server is running on port 35729
2024-03-03T11:56:36.552+05:30 : Tomcat started on port 8080 (http) with context path ''
2024-03-03T11:56:36.563+05:30 : Started SpringBootPracticeApplication in 3.159 seconds (process running for 3.997)
Add the following configuration your properties/yaml file:
1. application.properties
server.servlet.context-path=/my-context-path
2. application.yaml
server:
servlet:
context-path: /my-context-path
After making this change, your Spring Boot application will be accessible using the new context path. For example, if you set the context path to /my-context-path
, your endpoints will be available under http://localhost:8080/my-context-path
.
2024-03-03T12:16:39.623+05:30 : Tomcat initialized with port 8080 (http)
2024-03-03T12:16:39.642+05:30 : Starting service [Tomcat]
2024-03-03T12:16:39.642+05:30 : Starting Servlet engine: [Apache Tomcat/10.1.18]
2024-03-03T12:16:39.720+05:30 : Initializing Spring embedded WebApplicationContext
2024-03-03T12:16:39.721+05:30 : Root WebApplicationContext: initialization completed in 1894 ms
2024-03-03T12:16:40.315+05:30 : LiveReload server is running on port 35729
2024-03-03T12:16:40.380+05:30 : Tomcat started on port 8080 (http) with context path '/my-context-path'
2024-03-03T12:16:40.392+05:30 : Started SpringBootPracticeApplication in 3.26 seconds (process running for 4.322)
References
Similar Posts
- Spring AOP + AspectJ @Pointcut Annotation Example
- Spring AOP + AspectJ @Before, @After, @AfterReturning, @AfterThrowing, and @Around Annotation Example
- Spring Boot Security- Change default username and password parameter
- Spring AOP around advice (MethodInterceptor) example using XML configuration
- Spring Boot RESTful Web Service with JPA and MySQL