Newsletter |
Spring Boot – How to Change Default Context Path
Firstly what is this context path? simply its our application name. Generally while we are hitting any application in the browser, we will write the URL with the application name(context) right?
I mean…
http://localhost:<port>/<application_name or context_path>/operation_name
But if you check Spring Boot RESTful Web Service Example we haven’t included any context path, we directly ran the application with the path we have given in @RequestMapping, ( go back and have a look once ).
Spring Boot by default consider the context path as ‘/‘ so we no need to give our application name or context path, but in real-time we should use some context path for the applications. In this article I will show you how to change default spring boot application context path ‘/‘ to your application name.
In Spring Boot, we can change application default context path in two ways…
- Using applications.properties
- Using Java code changes
Its very simple just like changing tomcat port number in the previous article 🙂
Using application.properties
Create application.properties in your application src/main/resources and write this line..
server.contextPath=/yourApplicationName
Using Java Code Changes
package com.java4s.app.server; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.stereotype.Component; @Component public class Server implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setContextPath("/yourApplicationName"); } }
Now we have to run the application by hitting http://localhost:8080/yourApplicationName/, you can download this example and give a try 😉
Note: If we use both java and properties file approaches, spring boot will consider java only.
You Might Also Like
::. About the Author .:: | ||
Hi Siva,
I think its server.context-path, not the server.contextPath.
Please correct me, if i am wrong 🙂
Anyway excellent article.
Hi Dude, now it's became as – server.servlet.context-path=/yourApplicationName
Excellent tutorials for beginners. All the pages are fantastic and point to point. Keep up the great work!
Hi Siva,
You are really super bro. The way you explain the concept is really easy. Thanks for the super article.