How to Change Ports In Spring Boot?

12 minutes read

In Spring Boot, you can change the default port by modifying the application properties file. Here's how you can do it:


By default, Spring Boot uses port 8080. To change it, open the application.properties file in the src/main/resources directory of your Spring Boot project.


Add the following line in the file:

1
server.port=<port_number>


Replace <port_number> with the desired port number you want to use, for example, server.port=9090.


Save the file. Now, when you run your Spring Boot application, it will start on the specified port.


You can also configure the port programmatically by creating a configuration bean. Create a new class and annotate it with @Configuration.


Inside the class, create a method that returns an instance of EmbeddedServletContainerCustomizer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PortConfiguration {
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (ConfigurableEmbeddedServletContainerFactory container) -> {
            ((TomcatEmbeddedServletContainerFactory) container).setPort(<port_number>);
        };
    }
}


Replace <port_number> with the desired port number you want to use, for example, setPort(9090).


Save the file. Now, when you run your Spring Boot application, it will start on the specified port.


Both of these methods allow you to change the default port in Spring Boot and run your application on a different port of your choice.

Best Spring Boot Books to Read in November 2024

1
Full Stack Development with Spring Boot and React: Build modern and scalable web applications using the power of Java and React, 3rd Edition

Rating is 5 out of 5

Full Stack Development with Spring Boot and React: Build modern and scalable web applications using the power of Java and React, 3rd Edition

2
Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications

Rating is 4.9 out of 5

Spring Boot Persistence Best Practices: Optimize Java Persistence Performance in Spring Boot Applications

3
Spring Boot in Action

Rating is 4.8 out of 5

Spring Boot in Action

4
Spring Boot: Up and Running: Building Cloud Native Java and Kotlin Applications

Rating is 4.7 out of 5

Spring Boot: Up and Running: Building Cloud Native Java and Kotlin Applications

5
Learning Spring Boot 3.0: Simplify the development of production-grade applications using Java and Spring, 3rd Edition

Rating is 4.6 out of 5

Learning Spring Boot 3.0: Simplify the development of production-grade applications using Java and Spring, 3rd Edition

6
Spring in Action, Sixth Edition

Rating is 4.5 out of 5

Spring in Action, Sixth Edition

7
Modern API Development with Spring and Spring Boot: Design highly scalable and maintainable APIs with REST, gRPC, GraphQL, and the reactive paradigm

Rating is 4.4 out of 5

Modern API Development with Spring and Spring Boot: Design highly scalable and maintainable APIs with REST, gRPC, GraphQL, and the reactive paradigm

8
Spring Boot and Angular: Hands-on full stack web development with Java, Spring, and Angular

Rating is 4.3 out of 5

Spring Boot and Angular: Hands-on full stack web development with Java, Spring, and Angular


What is the default port used by Spring Boot?

The default port used by Spring Boot is 8080.


Can you use a random port for testing in Spring Boot?

Yes, you can use a random port for testing in Spring Boot. By default, when running a Spring Boot application test, the application starts on a random port to avoid conflicts with other running instances.


To use a random port, you can use the @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) annotation on your test class. This annotation ensures that the application starts on a random available port.


Additionally, you can use the @LocalServerPort annotation to inject the actual port number into a test field or method parameter. This allows you to retrieve the dynamically assigned port during the test execution.


How do you configure Tomcat connector properties for a port in Spring Boot?

To configure Tomcat connector properties for a specific port in Spring Boot, you can use server.<port>. prefix followed by the Tomcat connector property name in your application.properties or application.yml file.


Here is an example of how you can configure the connector properties for a specific port (e.g., port 8080) in application.properties file:

  1. Open the application.properties file.
  2. Add the following configuration properties:
1
2
server.port=8080
server.8080.<connector-property-name>=<value>


Replace <connector-property-name> and <value> with the actual Tomcat connector property name and value that you want to configure. You can refer to the Tomcat documentation for a list of available connector properties.


For example, to configure the maximum number of threads in the thread pool for the connector on port 8080, you can use the following configuration:

1
2
server.port=8080
server.8080.tomcat.max-threads=100


Save the changes, and Spring Boot will configure the Tomcat connector property for the specified port according to your configuration.


Note: You can also use the application.yml file to configure these properties with the YAML syntax instead of .properties format.

Best Cloud Providers to Host Java Spring Boot in 2024

1
AWS

Rating is 5 out of 5

AWS

2
DigitalOcean

Rating is 4.9 out of 5

DigitalOcean

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


Can you bind the application to a specific IP address in Spring Boot?

Yes, you can bind the application to a specific IP address in Spring Boot by configuring the server.address property in the application.properties file.


For example, to bind the application to the IP address 192.168.0.100, you can add the following line to your application.properties file:

1
server.address=192.168.0.100


Alternatively, you can set the property programmatically by using the ServerProperties bean. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@SpringBootApplication
public class YourApplication implements CommandLineRunner {

    @Autowired
    private ServerProperties serverProperties;

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        serverProperties.setAddress(InetAddress.getByName("192.168.0.100"));
    }
}


Remember to replace "192.168.0.100" with the desired IP address to which you want to bind your Spring Boot application.


How do you configure a different port for different environments in Spring Boot?

To configure a different port for different environments in Spring Boot, you can follow these steps:

  1. In your application.properties or application.yml file, define the default port for your application. For example, server.port=8080.
  2. Create separate configuration files for each environment, such as application-dev.properties or application-dev.yml, application-prod.properties or application-prod.yml, etc.
  3. In each environment-specific configuration file, override the server.port property with the desired port number specific to that environment. For example, in application-dev.properties: server.port=8081 and in application-prod.properties: server.port=8082.
  4. In your main Spring Boot application class, annotated with @SpringBootApplication, load the environment-specific configuration based on the active profile. This can be done using the @PropertySource annotation, SpringApplication.setDefaultProperties() method, or spring.config.name and spring.config.location properties. Here is an example using @PropertySource:
1
2
3
4
5
6
7
@SpringBootApplication
@PropertySource("classpath:application-${spring.profiles.active}.properties")
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}


Note: Make sure you have the appropriate dependencies (such as spring-boot-starter-web, spring-boot-starter) in your pom.xml or build.gradle file.

  1. Set the active profile for your application, either programmatically or through configuration. This can be done using the spring.profiles.active property in application.properties or application.yml, or by setting this property in your IDE or command line arguments.


Now, when you run your Spring Boot application with different profiles (e.g., dev, prod), it will pick up the corresponding environment-specific configuration file and use the configured port for that environment.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To integrate Spring Boot with Angular, the following steps can be followed:Create a new Spring Boot project: Start by setting up a new Spring Boot project using your preferred IDE or Spring Initializer. Include the necessary dependencies for web and data. Set ...
To connect Spring Boot to MySQL, you need to follow these steps:First, make sure you have MySQL installed and running on your system. In your Spring Boot project, open the application.properties file. Add the following properties to the file: spring.datasourc...
To stop a Spring Boot application from the command line, you need to perform the following steps:Identify the process ID (PID) of the running Spring Boot application. You can do this by using the jps command. Open the command prompt and type jps -l. Look for t...