3 minutes

How to Add Context Path to a Spring Boot Application

By Nilanchala @nilan, On Sep 17, 2023 Spring Boot 2.68K Views

In a Spring Boot application, the context path represents the base URL for accessing your application. By default, it's set to '/',

For example, in the following code snippet

@RequestMapping(value = "/products")
public class ProductController {

    public List<Product> getProducts() {
        return ResponseEntity.ok(productService.getProducts());

    @GetMapping(value = "/{productId}")
    public Product getProduct(@PathVariable(value = "id") Long id) {        
			//Your service logic goes here..
      return product;

To test this locally, we need to hit


But sometimes you might want to change the path to make it more meaningful or to avoid conflicts. So, how do we do that?”

To change the context path, you need to add the following property to your file.


Alternatively, if you're using the application.yaml file, you can do this

     context-path: '/api/1.0'

Now with this, you run your application and you will be able to access the endpoints with a new URL


I hope this helps! Let me know if you have any other questions.

nilan avtar


I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.