Home » Spring Boot Change Port

Spring Boot Change Port

by Online Tutorials Library

Spring Boot Change Port

The Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot.

We can change the port in Spring Boot by using the following interfaces and properties files:

  • Using application.properties file
  • Using application.yml file
  • Using EmbeddedServletContainerCustomizer Interface
  • Using WebServerFactoryCustomizer Interface
  • Using Command-Line Parameter

Using application.properties file

We recommend you to use the application.properties file if you want to change the default port. Because it is an easy and faster way to overwrite default values. We use the server.port property to overwrite the default property.

For example, if we want to change default port 8080 to 8082, specify the property in application.properties file.

application.properties

We can also set the port property to 0. It scans the random port for the application. It uses a new port whenever we restart our application.

application.properties

Using application.yml file

Similarly, we can also change the default port by using a yml file. Use either application.properties or application.yml file, both the files works in the same manner.

application.yml

Using EmbeddedServletContainerCustomizer Interface

If you are using Spring Boot 1.x version, it provides an interface EmbeddedServletContainerCustomizer to change the default port.

EmbeddedServletContainerCustomizer Interface

By using the EmbeddedServletContainerCustomizer, we can customize auto-configured embedded servlet containers. All the beans of this type get a callback with the container factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages. It is defined in the org.springframework.boot.context.embedded package.

The interface contains a method called customize(). It allows us to customize and specify ConfigurableEmbeddedServletContainer. It parses a parameter called container that we want to customize.

Syntax

ConfigurableEmbeddedServletContainer Interface

It is an interface that reflects the changes in the EmbeddedServletContainerFactory interface (factory interface used to create EmbeddedServletContainers). It is defined in the org.springframework.boot.context.embedded package. It contains a method to change the port called the setPort() method.

setPort() method

The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int.

Syntax

In the following example, we have created a class named ServerCustomizer and implements the EmbeddedServletContainerCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 8097.

ServerCustomizer.java

Using WebServerFactoryCustomizer Interface

Spring Boot 2.x version provides WebServerFactoryCustomizer interface to change the default port. It defined in the package org.springframework.boot.web.server. It parses a parameter T of type web server factory.

Syntax

The interface contains a method called customize(). It allows us to customize web server factories. It parses a parameter called factory that we want to customize. All the beans of this type get a callback with the server factory before starting the container itself. Therefore, we can set the port, addresses, and even error pages.

Syntax

WebServerFactory Interface

It is a tagging interface for factories. It is defined in org.springframework.boot.web.server package. It creates a WebServer.

ConfigurableWebServerFactory

It is an interface that configures web server factory. It is defined in the package org.springframework.boot.web.server. It extends WebServerFactory and ErrorPageRegistory. It contains a method to change the port called the setPort() method.

setPort()

The setPort() method configures the port of embedded servlet container should listen on. When we do not specify the port, it uses the default port 8080. If we want to disable the auto-start feature of the embedded server, use the port -1. The port -1 represents that it will not listen to any port but start the web application context. The method parses a parameter port (the port to set) of type int.

Syntax

Note: This setPort() method is of interface ConfigurableWebServerFactory

In the following example, we have created a class named ServerCustomizer that implements the WebServerFactoryCustomizer Interface. We have overridden the customize() method and invoke the setPort() method that sets the port 9001.

ServerCustomizer.java

Using Command Line Parameter

We can also change the port in Spring Boot by using the command line parameter. We must follow the steps given below:

  • Open any Spring Boot application.
  • Click on Run menu and select Run Configurations Or right-click on the application file -< Run As -< Run Configurations. Run Configurations window appears on the screen.
    Spring Boot Change Port
    Run Configurations window appears on the screen.
    Spring Boot Change Port
  • Select the application file in which you want to change the port. In our case, we want to change the port of SpringBootHelloWorldExampleApplication, so we have selected it.
  • Click on the Arguments tab.
    Spring Boot Change Port
  • Write -Dserver.port=9001 in the VM arguments field. You can specify your own port instead of 9001.
    Spring Boot Change Port
  • Now, click on Apply and Run button, respectively.
    After clicking on the Run button, the application starts running. We can see the console to see on which port server is running, as shown below.
    Spring Boot Change Port
  • Open the browser and invoke the URL http://localhost:9001. It runs the application on port 9001.
    Spring Boot Change Port

Next TopicSpring

You may also like