How to set Context Path in Spring Boot

By default, the web application Context Path is “/”. This example shows you 2 ways to set Context Path in Spring Boot: by Java Code and by properties file.

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview
1. Ways to do

For example, if we wanna set Context Path is /javasampleapp, port 8080, just choose one of 2 simple ways:

– By Java Code, we create a Component that implements EmbeddedServletContainerCustomizer, then override customize() method:

– By properties file, we just add these code:

If we wanna create referenced links in JSP pages, just use:

2. Project Structure

WebController is a Controller which has request mapping methods to display our page.
ConfigContainer is a Component which is used to set Context Path.
webapp folder contains all necessary JSP file and static resource. contains settings for prefix and suffix for JSP files and Context Path.
Dependencies for Spring Boot and Tomcat Embed Jasper in pom.xml.

Remember that we should only choose one of 2 ways: using ConfigContainer or using Context Path in

III. Practice
1. Dependency

For War Packaging:

Note: (for another case)
If you create a Spring Starter Project with Jar Packaging, you should change


Then, extends @SpringBootApplication Class with SpringBootServletInitializer and add the code below:

2. Create jsp file and static resource

– Under src/main/webapp folder, create new folder named WEB-INF and jsps inside, then create home.jsp and about.jsp:



– Next, create main.css in webapp/css:

3. Set prefix and suffix resolution

Under src/main/resources, open and add:

We have another way to set prefix and suffix, just follow the steps below:
– add dependency to pom.xml:

– then create a Bean:

4. Set Context Path

Way 1: Open and add:

Way 2: Under package config, create ConfigContainer Class that implements EmbeddedServletContainerCustomizer:

5. Create Web Controller

Under package controller, create WebController class:

6. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:




IV. Source Code


By JavaSampleApproach | February 24, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *