DEV Community


Posted on • Updated on • Originally published at

How to Add Context Path to a Spring Boot Application

In this article, we will explore how to add a context path to your Spring Boot application.

Let's dive right in!

First, let's understand what a context path is.

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;
Enter fullscreen mode Exit fullscreen mode

To test this locally, we need to hit

Enter fullscreen mode Exit fullscreen mode

But sometimes, you might want to change the path to make it more meaningful or to avoid conflicts.

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

Enter fullscreen mode Exit fullscreen mode

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

     context-path: '/api/1.0'
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

I hope this helps! Let me know if you have any other questions in the comments below.

Top comments (0)