Newsletter

Spring Boot – How to Change Default Context Path

Spring-Boot-Tutorials » on Nov 26, 2017 { 4 Comments } By Sivateja

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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

4 Responses to “Spring Boot – How to Change Default Context Path”
  1. Dheeresh Cool says:

    Hi Siva,

    I think its server.context-path, not the server.contextPath.
    Please correct me, if i am wrong 🙂
    Anyway excellent article.

  2. Rishikant Sharma says:

    Excellent tutorials for beginners. All the pages are fantastic and point to point. Keep up the great work!

  3. Swapnil says:

    Hi Siva,

    You are really super bro. The way you explain the concept is really easy. Thanks for the super article.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.