How to Change the Default Port in Spring Boot

1. Overview

Spring Boot provides sensible defaults for many configuration properties. But we sometimes need to customize these with our case-specific values.

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

In this quick tutorial, we’ll cover several ways to achieve this.

2. Using Property Files

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 starts on port 8080.

So, let’s see how to provide a different value in an application.properties file:

server.port=8081

Now the server will start on port 8081.

And 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.

2.1. Environment-Specific Ports

If we have an application deployed in different environments, we may want it to run on different ports on each system.

We can easily achieve this by combining the property files approach with Spring profiles. Specifically, we can create a property file for each environment.

For example, we’ll have an application-dev.properties file with this content:

server.port=8081

Then we’ll add another application-qa.properties file with a different port:

server.port=8082

Now, the property files configuration should be sufficient for most cases. However, there are other options for this goal, so let’s explore them as well.

3. Programmatic Configuration

We can configure the port programmatically either by 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:

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

Next, to customize the server configuration, we have to implement the WebServerFactoryCustomizer interface:

@Component
public class ServerPortCustomizer 
  implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
 
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(8086);
    }
}

Note that this applies to the Spring Boot 2.x version.

For Spring Boot 1.x, we can similarly implement the EmbeddedServletContainerCustomizer interface.

4. Using Command-Line Arguments

When packaging and running our application as a jar, we can set the server.port argument with the java command:

java -jar spring-5.jar --server.port=8083

or by using the equivalent syntax:

java -jar -Dserver.port=8083 spring-5.jar

5. Order of Evaluation

As a final note, let’s look at the order in which these approaches are evaluated by Spring Boot.

Basically, the configurations priority is

  • embedded server configuration
  • command-line arguments
  • property files
  • main @SpringBootApplication configuration

6. Conclusion

In this article, we saw how to configure the server port in a Spring Boot application.

As always, the source code for the examples is available over on GitHub.

Related posts:

Java Program to Implement the Program Used in grep/egrep/fgrep
How to Convert List to Map in Java
Getting Started with Custom Deserialization in Jackson
So sánh ArrayList và LinkedList trong Java
“Stream has already been operated upon or closed” Exception in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
So sánh Array và ArrayList trong Java
Beans and Dependency Injection
Introduction to Eclipse Collections
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement LinkedHashMap API
Circular Dependencies in Spring
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java – Convert File to InputStream
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Read an Outlook MSG file
Java Program to Generate Randomized Sequence of Given Range of Numbers
Spring @RequestMapping New Shortcut Annotations
Getting a File’s Mime Type in Java
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Jackson – Change Name of Field
Lớp HashMap trong Java
Number Formatting in Java
Auditing with JPA, Hibernate, and Spring Data JPA
Iterable to Stream in Java
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Quick Sort with Given Complexity Constraint
So sánh HashMap và Hashtable trong Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?