English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot Port Change

The Spring Boot framework provides the default embedded server (Tomcat) for running Spring Boot applications. It runs on port 8080 to run. You can change the port in Spring Boot.

We can change the port in Spring Boot. By using the following interface and property files:

using application.properties file using application.yml file using EmbeddedServletContainerCustomizer interface using WebServerFactoryCustomizer interface usingcommand line arguments

using the application.properties file

If you want to change the default port, it is recommended that you use the application.properties file. Because this is a quick and easy way to override default values. We use server.port attribute to override the default attribute.

For example, if you want to change the default port808Change 0 to8082Please specify this property in the application.properties file.

application.properties

server.port=8082

We can also set the port attribute to 0. It will scan a random port of the application. Every time we restart the application, it will use a new port.

application.properties

server.port=0

using the application.yml file

Similarly, we can also use yml file to change the default port. application.properties or application.yml file, the two files work in the same way.

application.yml

server:
      port:8082

using the EmbeddedServletContainerCustomizer interface

If you are using Spring Boot 1.x version, which will provide an interface EmbeddedServletContainerCustomizer to change the default port.

EmbeddedServletContainerCustomizer interface

By using EmbeddedServletContainerCustomizer, we can customize the automatically configured embedded Servlet container. Before the container itself starts, all such types of Beans will receive callbacks through the container factory. Therefore, we can set port, addressand Uniform error pages. It is org.springframework.boot.context.embedded defined in the package.

interface contains a named customize(). It allows us to customize and specify ConfigurableEmbeddedServletContainer . It parses the method we want to customize named containerwith parameters.

Syntax

void customize(ConfigurableEmbeddedServletContainer container)

ConfigurableEmbeddedServletContainer interface

This reflects EmbeddedServletContainerFactory interface (used for creating an interface for changes in the EmbeddedServletContainers factory interface) . It is org.springframework.boot.context.embedded defined in the package. It contains a method for changing the port called setPort() method.

setPort() method

The setPort() method configures the port that the embedded servlet container should listen on. If no port is specified, it will use the default port 8080 . If you want to disable the automatic startup feature of the embedded server, please use the port -1 . Port-1indicates that it will not listen on any port but start the Web application context. This method parses the int parameter port (to be set por t).

Syntax

void setPort(int port)

In the following example, we create a named ServerCustomizer class, and has implemented the EmbeddedServletContainerCustomizer interface. We have overridden the customize() method and called the method to set the port 8097 the setPort() method.

ServerCustomizer.java

@Component
public class ServerCustomizer implements EmbeddedServletContainerCustomizer 
{
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) 
    {
        container.setPort(8097);
    }
}

using the WebServerFactoryCustomizer interface

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

Syntax

public interface WebServerFactoryCustomizer<T extends WebServerFactory<

interface contains a named customize()method. It allows us to customize the Web server factory. It parses the named factory parameters. Before the container itself starts, all such types of Beans will be called back through the server factory. Therefore, we can set port, addressand error pages.

Syntax

void customize(T factory)

WebServerFactory interface

This is the marker interface of the factory. It is in org.springframework.boot.web.server defined. It creates a WebServer .

ConfigurableWebServerFactory

This is the interface for configuring the Web server factory. It is in the package org.springframework.boot.web.server defined. It extends WebServerFactory and ErrorPageRegistry . It contains a method to change the port called setPort()method.

setPort()

setPort() method to configure the port that the embedded servlet container should listen on. When we do not specify a port, it will use the default port 8080 . If you want to disable the automatic startup feature of the embedded server, please use the port -1 . Port-1indicates that it will not listen on any port but start the Web application context. This method parses the int parameter port (to be set port )。

Syntax

void setPort(int port)
Note: The interface of this setPort() method is ConfigurableWebServerFactory.

In the following example, we create a named ServerCustomizer This class implements the WebServerFactoryCustomizer interface. We have overridden the customize() method and called the setPort() method to set the port 9001 .

ServerCustomizer.java

@Component
public class ServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory< 
{
    @Override
    public void customize(ConfigurableWebServerFactory factory) 
    {
        factory.setPort(9001);
    }
}

Using Command Line Arguments

We can also change the port in Spring Boot by using command line arguments. We must follow the following steps:

to open any Spring Boot application. clickRunmenu, then selectRun Configuration,or right-click on the application file- <Run Mode-<  Run Configuration. The "Run Configuration" window will be displayed on the screen.  
   
 Run Configuration Windowthat appears on the screen.  
 
Select the application file to change the port in. In our example, we want to change SpringBootHelloWorldExampleApplication the port, so we selected it. clickparameterstab.
in VM parametersin the fieldwrite -Dserver.port = 9001 . You can specify your own port instead of9001.
Now, click onApplicationandRunbutton.
After clicking the "Run" button, the application starts running. We can see the console to check which port the server is running on, as shown below.
Open the browser and call the URL http://localhost: 9001. It is running on port 9001 Run the application here.