Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In a Spring Boot application, the default port for the embedded Tomcat server is 8080
. If you wish to change the default port (or configure other server settings), you can do so using the application.properties
or application.yml
file. Here's how you can change the port for your Spring Boot application:
application.properties
:Open (or create) the application.properties
file inside the src/main/resources
directory of your project and add:
server.port=8081
Replace 8081
with the desired port number.
application.yml
:If you're using the application.yml
file for configuration, the equivalent configuration would be:
server: port: 8081
Again, replace 8081
with the desired port number.
You can also specify the port when you run your Spring Boot application from the command line by using the --server.port
argument:
java -jar your-spring-boot-app.jar --server.port=8081
Or, if you're using the Spring Boot Maven plugin:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8081'
Spring Boot properties can also be set via environment variables. For instance, you can set the SERVER_PORT
environment variable to the desired port number before running your application.
export SERVER_PORT=8081 java -jar your-spring-boot-app.jar
Remember, command-line properties will always take precedence over property files, so if both are specified, the command-line value will be used.
Once you've made these changes and restarted your application, your Spring Boot application will now run on the new port. Make sure the port you choose is available and not being used by another service on your machine.
Configuring port number in Spring Boot:
application.properties
or application.yml
.# application.yml server: port: 9090
Override default port in Spring Boot:
// Java Configuration import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() { return factory -> factory.setPort(9090); } }
Command line options to change Spring Boot port:
java -jar my-application.jar --server.port=9090
Environment properties for setting port in Spring Boot:
# application.properties SPRING_APPLICATION_JSON='{"server.port":9090}'
Random port assignment in Spring Boot:
# application.yml server: port: 0
External configuration for port in Spring Boot:
application.properties
, application.yml
).# external-configuration.yml server: port: 9090
java -jar my-application.jar --spring.config.location=classpath:/external-configuration.yml
Setting server.port in application.properties in Spring Boot:
server.port
property in application.properties
.# application.properties server.port=9090
Configuring multiple ports in Spring Boot:
# application.yml server: port: 8080 management: port: 9090