A Step-By-Step Guide to Changing Server Port in a Spring Boot Application

Introduction: Changing Server Port in a Spring Boot Application

In a Spring Boot application, the server port on which your application runs is a crucial configuration parameter. By default, Spring Boot applications use port 8080. However, there are scenarios where you might need to change this port, such as avoiding port conflicts or running multiple applications on the same server. In this article, we’ll see the stepwise guide to Changing Server Port in a Spring Boot Application. We’ll cover various methods and provide code snippets, along with solutions to potential errors that might arise during the process.

Understanding the Default Port

By default, Spring Boot applications run on port 8080. However, this can be problematic if the port is already in use, or you want to run multiple Spring Boot applications on the same server. Changing the server port allows you to address these issues effectively.

Changing the Port via Application Properties

One of the simplest ways to change the server port is by using application.properties or application.yml configuration files. You can specify the desired port as follows:

Using `application.properties`

server.port=your_desired_port

Using `application.yml`

server:
  port: your_desired_port

Replace `your_desired_port` with the port number you want to use for your Spring Boot application. This approach is easy and provides flexibility.

How is Java Pass by Value and Not by Reference [4 Examples]

Command-Line Arguments for Port Configuration

Another way to change the server port is by using command-line arguments when running your Spring Boot application. This method can be particularly useful when you want to configure the port dynamically.

To specify the port using a command-line argument, you can run your Spring Boot application with the following command:

java -jar your-application.jar --server.port=your_desired_port

Replace `your-application.jar` with the name of your JAR file and `your_desired_port` with the desired port number. This approach allows you to override the port defined in your application.properties or application.yml file.

Programmatically Changing the Port

In some cases, you might need to change the server port programmatically. For example, you may want to set the port based on some runtime conditions. To do this, you can create a configuration class in your Spring Boot application.

Here’s an example of how to programmatically change the port using Java configuration:

import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServerPortConfig {

    @Bean
    public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowCustomizer() {
        return factory -> factory.setPort(your_desired_port);
    }
}

In this code, we create a `ServerPortConfig` class and define a custom `WebServerFactoryCustomizer` for Undertow, which is one of the embedded web servers used by Spring Boot. You can choose a different web server if your application uses something other than Undertow. Replace `your_desired_port` with the port number you want to use.

An Easy Step-By-Step Guide to Changing Server Port in a Spring Boot Application

Common Errors and Solutions

Changing the server port in a Spring Boot application is usually straightforward, but you may encounter some common errors. Let’s explore these errors and provide solutions:

Error 1: Port Already in Use

If you attempt to start your Spring Boot application on a port that’s already in use, you’ll receive an error message like:

java.net.BindException: Address already in use

Solution: Choose a different port that is not currently in use.

Simple Guide To Dockerize Java Application Maven With Dockerfile [2020]

Error 2: Invalid Port Number

If you specify an invalid or non-numeric port number, you’ll encounter an error like:

java.lang.IllegalArgumentException: Port is outside the range 0-65535

Solution: Ensure that you provide a valid port number between 0 and 65535.

Error 3: Configuration Not Taking Effect

If your configured port is not taking effect, check the following:

– Verify that you’re modifying the correct `application.properties` or `application.yml` file.
– Make sure there are no typos or syntax errors in your configuration.

If using the command-line argument method, ensure that you’re specifying the `–server.port` option correctly.

If you’re changing the port programmatically, double-check your custom configuration class for any errors.

Official Documentation

https://docs.spring.io/spring-boot/docs/1.3.3.RELEASE/reference/html/howto-properties-and-configuration.html#howto-use-short-command-line-arguments

Conclusion

Changing the server port in a Spring Boot application is a straightforward process, and you have multiple options to achieve it. You can use application properties, command-line arguments, or programmatic configuration, depending on your requirements. However, always be cautious of potential errors, such as port conflicts or invalid port numbers, and ensure your configuration takes effect as expected. With the knowledge gained in this article, you can confidently configure the server port in your Spring Boot applications to suit your needs.
This ends the article on A Step-By-Step Guide to Changing Server Port in a Spring Boot Application.