Customizing Your Spring Boot Application- A Guide to Changing the Default Port to a Desired Custom Port

by liuqiyue

How to Alter Spring Boot Default Port to Custom Port

In the world of web development, Spring Boot has emerged as a popular framework for creating standalone, production-grade applications. One of the default configurations of Spring Boot is the port on which the application runs. By default, Spring Boot applications run on port 8080. However, there may be situations where you need to alter this default port to a custom one. This article will guide you through the process of how to alter the default port of a Spring Boot application to a custom port.

Understanding the Port Configuration

Before diving into the process, it’s essential to understand how the port configuration works in Spring Boot. The port on which a Spring Boot application runs is specified by the server.port property in the application.properties or application.yml file. By default, this property is set to 8080. However, you can easily change this value to any other port number you prefer.

Changing the Port in application.properties

To alter the default port in a Spring Boot application, you can modify the application.properties file located in the src/main/resources directory. Open the file and locate the server.port property. By default, it should look like this:

“`properties
server.port=8080
“`

Replace the value with the custom port number you want to use. For example, if you want to run your application on port 9090, change the property to:

“`properties
server.port=9090
“`

Save the changes and restart your Spring Boot application. Now, your application should be running on the custom port you specified.

Changing the Port in application.yml

If you are using the YAML format for your configuration files, you can change the port in the application.yml file. Locate the server port property under the server section and modify it as follows:

“`yaml
server:
port: 9090
“`

Again, save the changes and restart your application. Your Spring Boot application will now run on the custom port specified in the configuration file.

Using Environment Variables

Another way to alter the default port of a Spring Boot application is by using environment variables. This method is particularly useful when deploying applications to different environments, such as development, staging, and production. To use environment variables, you can set the SERVER_PORT variable in your environment or pass it as a command-line argument when starting the application.

For example, to set the port to 9090 using an environment variable, you can run the following command in your terminal:

“`bash
export SERVER_PORT=9090
./start.sh
“`

This will start your Spring Boot application on port 9090.

Conclusion

In this article, we discussed how to alter the default port of a Spring Boot application to a custom port. By modifying the application.properties or application.yml file, or using environment variables, you can easily change the port on which your Spring Boot application runs. This flexibility allows you to adapt your application to different deployment scenarios and requirements.

You may also like