Spring boot how to change server port

You are currently viewing Spring boot how to change server port
change server port in spring boot

Spring boot server port can be changed in a number of ways, by overriding in properties file, JVM argument, command line arguments, build script or programmatically. Spring boot applications generally include embedded servers like Tomcat or Jetty, the default server port for these embedded servers is 8080. Spring boot provides a number of hooks via which you can easily override this default server port. We have tried a number of ways for overriding server ports and listing here all the solutions which work well and answer the question that in spring boot how to change server port.

Spring boot server port is changed in application.properties or YML file easily. Server port is overridden using command line arguments, JVM arguments, spring application json or programmatically within the code. Spring boot application running via using commands like jar, gradle, maven or spring boot then all provides an option to override the spring boot embedded server port.

This article will provide a bonus factor. With each example, you will learn a new way to run spring boot applications. If you want to learn the different ways to run a spring boot application, then following examples will serve the purpose. So we can say the different ways to run spring boot application or the different ways to change spring boot server port are following:

1. Change spring boot server port in application.properties file

The spring boot application’s default server port can be overridden in application.properties. The property key name is server.port and which can be mentioned anywhere in application.properties file. For example, if we have to override default server port with value 8081 then in application.properties file, we will add following line

server.port=8081

Application.properties file use default value if no value provided

In application.properties file, you can mention the default value if a custom value is not supplied. In absence of custom value, the default value, mentioned earlier will be used.

server.port=${port:8081}

In this example, ‘port’ argument can be supplied as a JVM argument or environment variable. If ‘port’ value is present while starting application then it will override the default server port value. If ‘port’ value is absent then default value 8081, mentioned in application.properites file will be used.

2. Change spring boot server port in application.yml file

Application.yml properties provide similar functionality to override server port. Due to yml syntax, the syntax to mention server.port key differs but the functionality remains the same.

server
  port=8082

3. Change spring boot server port programmatically

Spring boot server port can be overriden programmatically too. Two options are available here. In the First option, you can pass it as default properties in the main method, which will get executed before starting the server. Another option is to implement the WebServerFactoryCustomizer interface and get a callback with the server factory to update configuration.

Following is the example, if you want to override the server port from the main method itself.

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

Alternatively you can use WebServerFactoryCustomizer interface. The  WebServerFactoryCustomizer is a Strategy interface for customizing the web server provided factories. A bean of WebServerFactoryCustomizer type will get a callback with the server factory before the server is started, here you can set the address, port, error pages etc.

@Component
public class CustomPort implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
  @Override
  public void customize(ConfigurableServletWebServerFactory server) {
     server.setPort(8084);
  }
}

4. Change spring boot server port via Passing command line argument 

Command line arguments can be passed in Spring boot application as application arguments. While running the jar using command ‘Java -Jar’ you can add the command line ‘-server.port’ argument at the end and provide the port number on which you want to run the application.

Syntax

java -jar <path/to/my/jar> --server.port=PORT_NUMBER

Example

java -jar demo1-0.0.1-SNAPSHOT.jar --server.port=8085

5. Change spring boot server port via JVM arguments

JVM arguments can be used to define the spring boot server port. While running Java commands, you can pass the server.port argument as a normal JVM argument, which will be used internally by Spring boot to override default server port. In this example, we are running spring boot jar with Java command and passing server.port argument as JVM argument.

Syntax

java -Dserver.port=PORT_NUMBER -jar <path/to/my/jar>

Example

java -Dserver.port=8086 -jar demo.0.1-SNAPSHOT.jar

6. Spring boot change server port within intellij 

Whatever IDE you are using, almost every IDE has an option to set JVm arguments. From IDE’s graphical interface it becomes easy to add JVM arguments. Some IDEs have this option as VM arguments or VM options. 

Following is the example screenshot for setting spring boot server boot as VM options in IntelliJ Idea IDE.

intellij idea set server port as VM options
Intellij idea set server port as VM options

7. Change spring boot server port via SPRING_APPLICATION_JSON

The SPRING_APPLICATION_JSON property is used by Spring boot application to use properties to specify as inline embedded JSON to be used in system property or environment variables. From the Unix shell command line, you can supply the SPRING_APPLICATION_JSON properties while running Java command. In the following example, we are passing server.port as json with a value of 8088 as server port.

Example

 SPRING_APPLICATION_JSON='{"server.port":8088}' java -jar sand-0.0.1-SNAPSHOT.jar

8. Change spring boot server port via spring.application.json as JVM argument

The spring.application.json property is treated as alternative to SPRING_APPLICATION_JSON, So its behavior is completely same, The spring.application.json property can be passed as JVM argument and can have a JSON value including any number of properties as well as nested type of objects as json. In following example we are passing server.port key with value 8089 as json to override spring boot default server port

Example

java -Dspring.application.json='{"server.port":8089}' -jar sand-0.0.1-SNAPSHOT.jar 

9. Change spring boot server port via spring.application.json as command line argument

The spring.application.json property with json value can be treated as a normal command argument and can be supplied to Java command while running spring boot application via command line. In the following example, we are passing server.port with value 8090 to override default server port in spring boot application.

Example:

java -jar sand-0.0.1-SNAPSHOT.jar --spring.application.json='{"server.port":8090}' 

10. Change spring boot server port via Spring Boot Gradle Plugin

In build.gradle, file you can also specify the server port to be used in spring boot’s embedded server. You can arguments, which will be automatically supplied to application when application is run using Spring boot’s bootRun command For this either you should be using Spring boot Gralde plugin or Spring dependency management

In build.gradle file add following snippet:

bootRun {
  args += ["--server.port=8091"]
}

After adding the above snippet, you should start your application using the bootRun command. Those who have not used it earlier, this is a task present in the gradle file automatically when you create a Spring boot project from Spring boot initializer.

In Gradle tasks you can find at the location, applicationName > tasks > application > bootRun. Double click on bootRun and you will see your spring boot application starts with the server port as mentioned by you in build.gradle.

11. Change spring boot server port for test cases

The annotation @SpringBootTest is provided by spring boot which is an alternative to spring-test @ContextConfiguration annotation. It’s very often, you want to run test cases or integration tests on different ports, as on the original port the application may already be running. 

By default, the server will not be started by @SpringBootTest annotation. The webEnvironment attribute of @SpringBootTest annotation is used to specify how to run the test cases. It can have multiple values, but we will stick to our focus only to set server port value and keeping other things out of discussion. 

Note:Before proceeding further, you should check the version of JUnit, you are using. If you are creating test cases using JUnit 4, then you must have @RunWith(SpringRunner.class) annotation at test class level, otherwise the annotations will be ignored. For JUnit 5, you don’t need to do anything, as @SpringBootTest already internally does this work.

The webEnvironment has following cases to change server port.

1. RANDOM_PORT: With this option, a random number is used as a server port. The WebServerApplicationContext provides a real web environment, where embedded servers are started and a random port number is set as server port.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DemoTest {
  ….// test cases
}

2. DEFINED_PORT: Here the server port’s overridden value is used and in absence of any configuration, the default port 8080 is used. The WebServerApplicationContext gets loaded and provides a real web environment, the embedded servers are started with either defined server port or default server port.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=8092")
public class DemoTest {
  ….// test cases
}

12. Change spring boot server port via gradle command line arguments

Spring boot application can be run from gradle command and while running application using gradle command, we pass arguments to override the system properties. In our case, we want to pass the server port value to be used by the embedded server in the spring boot application.

./gradlew bootRun --args='--server.port=8093'

In addition to overriding system properties, we can also process custom arguments via gradle command line arguments. To process a custom argument, let’s add the following snippet to your build.gradle.

bootRun {
  if (project.hasProperty('args')) {
     args project.args.split(',')
  }
}

Once you have added above code then you can pass the custom arguments as ‘-P’, since we have used the name  ‘args’ in build.gradle then the following command can be used to pass the customer arguments.

 ./gradlew bootRun -Pargs=--server.port=8094

13. Change spring boot server port via maven command line arguments

Maven based spring boot can be started using spring-boot:run command. Along with this command you can pass runtime arguments. In our example, we can pass server port in following way:

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8095

Conclusion

We hope this article will help you somehow. We have tried to include every possible configuration or method to demonstrate the different ways in spring boot how to change server port.