How to Configure Port for a Spring Boot Application

How to configure port for a Spring Boot application

As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with

server.port=8090

For a random port use:

server.port=0

Similarly add application.yml in /src/main/resources/ with:

server:
port: 8090

How to configure port for a Spring Boot application

As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with

server.port=8090

For a random port use:

server.port=0

Similarly add application.yml in /src/main/resources/ with:

server:
port: 8090

In Springboot, is there any way to change the port other than setting it up in application.properties?

Programmatic Configuration
We can configure the port programmatically by either setting the specific property when starting the application or by customizing the embedded server configuration.

First, let's see how to set the property in the main @SpringBootApplication class:

@SpringBootApplication
public class CustomApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(CustomApplication.class);
app.setDefaultProperties(Collections
.singletonMap("server.port", "8083"));
app.run(args);
}
}

Next, to customize the server configuration, we have to implement the WebServerFactoryCustomizer interface:

@Component
public class ServerPortCustomizer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8086);
}
}

Note that this applies to Spring Boot 2.x version.

For Spring Boot 1.x, we can similarly implement the EmbeddedServletContainerCustomizer interface.

Using Command Line Arguments
When packaging and running our application as a jar, we can set the server.port argument with the java command:

  • java -jar spring-5.jar --server.port=8083

Or by using the equivalent syntax:

  • java -jar -Dserver.port=8083 spring-5.jar

Learn More at: https://www.baeldung.com/spring-boot-change-port

Note: If you have mentioned 8080 in application.properties but you want to run it on 8083 then it will work by giving the port number in command line arguments as like below,

  • java -jar -Dserver.port=8083 spring-5.jar

Change port of deployed Spring project on Wildfly

The server.port property only controls the listening port of an embedded server in a Spring boot app (tomcat, jetty, etc.).

The app's port in a standalone application server is specified during the deployment. The easiest way to do this on Wildfly is via the wildfly-maven-plugin (https://docs.jboss.org/wildfly/plugins/maven/latest/deploy-mojo.html):

<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>2.1.0.Final</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<filename>${project.build.finalName}.war</filename>
<hostname>127.0.0.1</hostname>
<port>8093</port> <!-- <<<<<<<< -->
<username>my-wildfly-user</username>
<password>my-wildfly-password</password>
</configuration>
</plugin>
<!-- ... -->
</plugins>
</build>


Related Topics



Leave a reply



Submit