Spring Boot How to Change Port and Context Path

In this article, we’ll discuss several ways to change the default port and context path in 
Spring Boot applications.

Let's first discuss how to change the default port for the embedded server, as we know by default, the embedded server start on port 8080.

Changing the Default Port for the Embedded server

A common use case is changing the default port for the embedded server.

Using application.properties File

The fastest and easiest way to customize Spring Boot is by overriding the values of the default properties.
For the server port, the property we want to change is server.port.
By default, the embedded server start on port 8080. Let’s see how we can provide a different value in an application.properties file:
Now the server will start on port http://localhost:8081.
Similarly, we can do the same if we’re using an application.yml file:
server:
  port: 8081
Both files are loaded automatically by Spring Boot if placed in the src/main/resources directory of a Maven application.

Programmatic Configuration

We can configure the port programmatically by either setting the specific property when starting the application or by customizing the embedded server configuration.
First, let’s see how to set the property in the main @SpringBootApplication class:
import java.util.Collections;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot2WebappJspApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Springboot2WebappJspApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "8081"));
        app.run(args);
        
    }
}

Using Command Line Arguments

When packaging and running our application as a jar, we can set the server.port an argument with the java command:
java -jar springboot2webapp.jar --server.port=8083
Or by using the equivalent syntax:
java -jar -Dserver.port=8083 springboot2webapp.jar

How to Change the Default Context Path?

There are several ways to change the default context path.

Using application.properties File

/src/main/resources/application.properties
server.port=8080
server.servlet.context-path=/springboot2webapp
By default, the context path is “/”. To change the context path, override and update server.servlet.context-path properties. The following examples update the context path from / to /springboot2webapp or http://localhost:8080/springboot2webapp Just like many other configuration options, the context path in Spring Boot can be changed by setting a property, i.e., server.servlet.context-path.
Note that this works for Spring Boot 2.x.
For Boot 1.x, the property is server.context-path.

Change Context Path Programmatically

SpringApplication has a method as setDefaultProperties() that is used to change spring boot default properties.
import java.util.Collections;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot2WebappJspApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Springboot2WebappJspApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.servlet.context-path", "/springboot2webapp"));
        app.run(args);
    }
}
These are simple ways we can change the default settings.

Comments