Spring Boot 2 How to Change Port and Context Path - RameshMF/spring-boot-developers-guide GitHub Wiki

In this article, we’ll discuss several ways to change the default post and context path.

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:

server.port=8081

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);
	}
}