How to Set Context Path in Spring Boot Application?
Last Updated :
24 Apr, 2025
The context path is a prefix to the URL path used to identify and differentiate between different context(s). In Spring Boot, by default, the applications are accessed by context path “/”. That means we can access the application directly at http://localhost:PORT/. For example
http://localhost:8080/
But, in some cases, you may wish to change the context path of an application. For example in production, you need to deploy the application under some context root, so that you can refer to the URLs for other places. And in the Spring Boot application, you can modify/set the default context path using the following property in the application.properties file
server.servlet.context-path=/YourContextPath
Let’s understand this concept with a sample Spring Boot application.
Example Spring Boot Project
Step 1: Create a New Spring Boot Project in Spring Initializr
To create a new Spring Boot project, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things
- Project: Maven
- Language: Java
- Packaging: Jar
- Java: 17
Please choose the following dependencies while creating the project.
- Spring Boot DevTools
- Spring Data JPA
- MySQL Driver
- Spring Web
Generate the project and run it in IntelliJ IDEA by referring to the above article.
Note: We have used the MySQL database in this project.
Step 2: Create Schema in MySQL Workbench and Put Some Sample Data
Go to your MySQL Workbench and create a schema named gfgmicroservicesdemo and inside that create a table called employee and put some sample data as shown in the below image. Here we have created 4 columns and put some sample data.
- id
- name
- email
- age
.png)
Now we are going to fetch Employee Data from Employee Table in our Spring Boot project. To do it refer to the following steps. Before moving to IntelliJ IDEA let’s have a look at the complete project structure for our Microservices.

Step 3: Make Changes in Your application.properties File
Now make the following changes in your application.properties file.
spring.datasource.url=jdbc:mysql://localhost:3306/gfgmicroservicesdemo
spring.datasource.username=put your username here
spring.datasource.password=put your password here
spring.application.name=employee-service
server.port=8080
# Set Your Context Path Here
server.servlet.context-path=/employee-service
You may also refer to the below image

Step 4: Create Your Entity/Model Class
Go to the src > main > java > entity and create a class Employee and put the below code. This is our model class.
Java
package com.gfg.employeaap.entity;
import jakarta.persistence.*;
@Entity
@Table (name = "employee" )
public class Employee {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column (name = "id" )
private int id;
@Column (name = "name" )
private String name;
@Column (name = "email" )
private String email;
@Column (name = "age" )
private String age;
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this .email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this .age = age;
}
}
|
Step 5: Create Your Repository Interface
Go to the src > main > java > repository and create an interface EmployeeRepo and put the below code. This is our repository where we write code for all the database-related stuff.
Java
package com.gfg.employeaap.repository;
import com.gfg.employeaap.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
}
|
Note: Please refer to the following article to know more about JpaRepository.
Step 6: Create an EmployeeResponse Class
Go to the src > main > java > response and create a class EmployeeResponse and put the below code.
Java
package com.gfg.employeaap.response;
public class EmployeeResponse {
private int id;
private String name;
private String email;
private String age;
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this .email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this .age = age;
}
}
|
Step 7: Create Your Service Class
Go to the src > main > java > service and create a class EmployeeService and put the below code. This is our service class where we write our business logic.
Java
package com.gfg.employeaap.service;
import com.gfg.employeaap.entity.Employee;
import com.gfg.employeaap.repository.EmployeeRepo;
import com.gfg.employeaap.response.EmployeeResponse;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
public class EmployeeService {
@Autowired
private EmployeeRepo employeeRepo;
@Autowired
private ModelMapper mapper;
public EmployeeResponse getEmployeeById( int id) {
Optional<Employee> employee = employeeRepo.findById(id);
EmployeeResponse employeeResponse = mapper.map(employee, EmployeeResponse. class );
return employeeResponse;
}
}
|
Step 8: Create an Employee Controller
Go to the src > main > java > controller and create a class EmployeeController and put the below code. Here we are going to create an endpoint “/employees/{id}” to find an employee using id.
Java
package com.gfg.employeaap.controller;
import com.gfg.employeaap.response.EmployeeResponse;
import com.gfg.employeaap.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping ( "/employees/{id}" )
private ResponseEntity<EmployeeResponse> getEmployeeDetails( @PathVariable ( "id" ) int id) {
EmployeeResponse employee = employeeService.getEmployeeById(id);
return ResponseEntity.status(HttpStatus.OK).body(employee);
}
}
|
Step 9: Create a Configuration Class
Go to the src > main > java > configuration and create a class EmployeeConfig and put the below code.
Java
package com.gfg.employeaap.configuration;
import com.gfg.employeaap.service.EmployeeService;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmployeeConfig {
@Bean
public EmployeeService employeeBean() {
return new EmployeeService();
}
@Bean
public ModelMapper modelMapperBean() {
return new ModelMapper();
}
}
|
Note: You may refer to these two articles
Before running the Microservice below is the complete pom.xml file. Please cross-verify if you have missed some dependencies
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< modelVersion >4.0.0</ modelVersion >
< parent >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-parent</ artifactId >
< version >3.0.2</ version >
< relativePath />
</ parent >
< groupId >com.gfg.employeaap</ groupId >
< artifactId >employee-service</ artifactId >
< version >0.0.1-SNAPSHOT</ version >
< name >employee-service</ name >
< description >Employee Service</ description >
< properties >
< java.version >17</ java.version >
</ properties >
< dependencies >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-data-jpa</ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-web</ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-devtools</ artifactId >
< scope >runtime</ scope >
< optional >true</ optional >
</ dependency >
< dependency >
< groupId >com.mysql</ groupId >
< artifactId >mysql-connector-j</ artifactId >
< scope >runtime</ scope >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-test</ artifactId >
< scope >test</ scope >
</ dependency >
< dependency >
< groupId >org.modelmapper</ groupId >
< artifactId >modelmapper</ artifactId >
< version >3.1.1</ version >
</ dependency >
</ dependencies >
< build >
< plugins >
< plugin >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-maven-plugin</ artifactId >
</ plugin >
</ plugins >
</ build >
</ project >
|
Step 10: Run Your Employee Microservice
To run your Employee Microservice src > main > java > EmployeeServiceApplication and click on the Run button. If everything goes well then you may see the following screen in your console. Please refer to the below image.

Here you can see in the highlighted green box that
Tomcat started on port(s): 8080 (http) with context path '/employee-service'
That means your context path has been set successfully.
Step 11: Test Your Endpoint in Postman
Now open Postman and hit the following URL
GET: http://localhost:8080/employee-service/employees/1
And you can see the following response
{
"id": 1,
"name": "Amiya",
"email": "ar@gmail",
"age": "25"
}
Similar Reads
How to Run Spring Boot Application?
Spring Boot is built on the top of Spring and contains all the features of Spring. And it is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and set
8 min read
How to Enable HTTPs in Spring Boot Application?
In today's world security is the most important parameter while implementing any task. In Spring Boot, security is paramount. If we are developing a web application using spring boot, then enabling the HTTPs (Hypertext Transfer Protocol Secure) is the most crucial step to protect important data and
4 min read
How to Create a Simple Spring Application?
Spring Boot is one of the most popular frameworks for building Java-based web applications. It is used because it simplifies the development process by providing default configurations and also reduces boilerplate code. In this article, we will cover the steps to create a simple Spring Boot applicat
2 min read
How Spring Boot Application Works Internally?
Spring Boot Application starts using a main method, like any other Java program, and the main method is called the "run" method i.e. SpringApplication.run(),. From this run method, the application context of IOC (Inversion Of Control) searches the class annotated with @Configuration annotation which
5 min read
How to Configure AuditListener in Spring Boot Application
Spring Boot Data provides support to transparently keep track of who created an entity or changed an entity and when these things happened. All records are maintained clearly. For example, if one user comes into the site and logs in to the site. Then those times are maintained in the database and al
4 min read
Containerizing Spring Boot Application
Java applications, known for their robustness sometimes become difficult to deploy and manage. This is where containerization comes into the picture. Packaging your Java app into a lightweight and self-contained independent unit can provide many benefits to the developers: PortabilityScalabilityFast
3 min read
How to Access Values From application.properties in Spring Boot?
In a Spring Boot application, application.properties is the central repository for defining configuration properties. These properties are accessible across the application to customize behavior. Accessing values from application.properties is a common requirement in Spring Boot projects. Spring Boo
3 min read
How to Run Your First Spring Boot Application in Eclipse IDE?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
3 min read
Spring Boot - Application Properties
Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex
3 min read
How to Run Your First Spring Boot Application in IntelliJ IDEA?
IntelliJ IDEA is an integrated development environment(IDE) written in Java. It is used for developing computer software. This IDE is developed by Jetbrains and is available as an Apache 2 Licensed community edition and a commercial edition. It is an intelligent, context-aware IDE for working with J
3 min read