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

  1. Spring Boot application.properties vs. application.yml
  2. Spring Boot- Disable Spring banner at startup

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.