Changing the port number of a Spring Boot application is a common requirement, especially when you need to run multiple applications on the same machine or avoid conflicts with other services. Spring Boot provides several ways to customize the port number. Here are the most common methods you can use:
1. Using application.properties
or application.yml
The simplest way to change the port number is by setting it in the application.properties
or application.yml
file. Depending on the format of your configuration file, you can specify the port as follows:
- In
application.properties
:
properties
server.port=8081
yaml
server:
port: 8081
This method is useful for applications where you want a persistent configuration and is easily editable.
2. Using Command-Line Arguments
Another way to change the port is by passing the server.port
property as a command-line argument when starting your Spring Boot application:
bash
java -jar myapp.jar --server.port=8081
This method is convenient when you want to quickly change the port number without modifying configuration files.
3. Using System Properties
You can also set the port number by passing it as a system property when running the application. This is done using the -D
flag:
bash
java -jar -Dserver.port=8081 myapp.jar
Setting the port number this way is particularly useful if you're running your application in an environment where you want to programmatically set the port before starting.
4. Using Environment Variables
Spring Boot allows you to configure the port number using environment variables. This method is especially useful in cloud environments, Docker containers, or other environments where you prefer to set configuration outside of the application code.
bash
export SERVER_PORT=8081
java -jar myapp.jar
Alternatively, you can also set the environment variable directly when running the application in a one-liner:
bash
SERVER_PORT=8081 java -jar myapp.jar
5. Embedded Servlet Container Customization
If you're using an embedded servlet container like Tomcat, Jetty, or Undertow, you can customize the port number programmatically in your Spring Boot application. Here’s how you can do this in your main application class:
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8081); // Set custom port
return factory;
}
}
This allows you to programmatically configure the port and other properties of the embedded servlet container.
Conclusion
Spring Boot provides multiple ways to change the default port number for your application. Depending on your needs—whether it's for local development, testing, or production—any of the methods outlined above can be used. Choose the method that fits your workflow best!