- Java spring server port
- 8. Change spring boot server port via spring.application.json as JVM argument
- 9. Change spring boot server port via spring.application.json as command line argument
- 10. Change spring boot server port via Spring Boot Gradle Plugin
- 11. Change spring boot server port for test cases
- 12. Change spring boot server port via gradle command line arguments
- 13. Change spring boot server port via maven command line arguments
- Conclusion
- Share this:
- How to configure server port in Spring Boot application
- How to Change the default port in Spring Boot
- Introduction
- 1. Change Port in Spring Boot using Properties Files
- 2. Programmatic Customization
- 3. Command Line Argument
- 4. Environment Specific Port
- 5. Configuration Evaluation Order
- Summary
Java spring server port
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.
SPRING_APPLICATION_JSON='' 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
java -Dspring.application.json='' -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.
java -jar sand-0.0.1-SNAPSHOT.jar --spring.application.json=''
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:
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.
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.
Share this:
How to configure server port in Spring Boot application
Spring Boot provided us with an embedded server feature. Before Spring Boot we have done lots of configurations to add a server with the Spring Application development. Sometimes when configuring to add a server for our project is giving some issues lots of time is consuming to fix these issues. Spring Framework provides a solution to this issue with Spring Boot Embedded Server. There is no need to configure to ada d server with Spring Boot Application for this we can use an Embedded server. This feature helps to develop a quick Spring application through Spring Boot.
If we don’t prefer to use an embedded server we can use an external server configuration for Spring Boot Application. This depends upon our requirements.
How to change the embedded server port number in the Spring Boot Application?
We can change the embedded Server Port number in the Spring Boot Application through the “application.properties” file. The embedded Server Port number is 8080 by default running.
Table of content:
1. Keep eclipse IDE ready
2. Create a Spring Boot Starter Project for this example of the Configure Server Port(Select Spring Web dependency)
3. Create a view page
4. Create a Controller class
5. Define the server port in the application.properties file for this example of the Configure Server Port
6. Run the Project
1. Keep eclipse IDE ready
2. Create a Spring Boot Starter Project for this example of the Configure Server Port(Select Spring Web dependency)
3. Create a view page
home.jsp
Hello Spring Boot App
Note: Create under the “WEB-INF/view” folder.
4. Create a Controller class
HomeController.java
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController < @GetMapping("/") public String msg() < //return a view page name return"home"; >>
→ @Controller annotation is to make normal as Controller class. This is the stereotype annotation. Stereotype annotation is giving a special role to the class as we use this “@Controller” annotation for the “HomeController” class to act as the Controller of this project.
→ This is the controller class with mapping i.e., @GetMapping(“/”). The mapping returns the view i.e., home.This “home” is a view page(home.jsp). The location the of view page is under “src\main\webapp\WEB-INF\view\” in the project.
5. Define the server port in the application.properties file for this example of the Configure Server Port
application.properties
# the servlet path server.servlet.context-path= /demo_mvc # MVC view prefix spring.mvc.view.prefix=/WEB-INF/view/ # MVC view suffix spring.mvc.view.suffix=.jsp #Server port server.port=8888
Note: server.port=number number: We can modify embedded the server port. In this port number, our project is running when we run our project.
→ Now our project is run with this port of the Server and the URL «http://localhost:8888/demo_mvc/”.
6. Run the Project
Right-click on this java class i.e., SpringBootWithSpringMvcApplication(automatic generated main class) of this project then click Run as Java Application
→ See the console Embedded Tomcat Server is started on Port number 8888. Go to the web browser and type the “http://localhost:8888/demo_mvc/”.
This example is explained:
• How to change the embedded server port number?
• How can we check on which port number the embedded server is started?
How to Change the default port in Spring Boot
In this short post, we are going to cover the common ways to change the default port in Spring Boot.
Introduction
Spring Boot comes with the ability to give sensible defaults based on the application configuration. For example, with web application, it comes with an embedded servlet container (tomcat) with defaults values configured for the servlet container. There are a few cases where we like to override these values with our custom values. In this post, we are going to learn the different options to change the default port in Spring Boot application.
1. Change Port in Spring Boot using Properties Files
Spring Boot provides a flexible way to configure our application using a property file. To change the default port, we need to set the desired port number using the server.port properties either through application.properties or application.yml file.Set server.port property in application.properties file.
Set server port property in application.yml file
Changing the default port in Spring Boot using application.properties is the most common and flexible way.
2. Programmatic Customization
We have the option to programmatically configure your embedded servlet container. To do this, create a Spring bean which implements the WebServerFactoryCustomizer interface.
@Component public class CustomizationPort implements WebServerFactoryCustomizer < @Override public void customize(ConfigurableServletWebServerFactory server) < server.setPort(9001); >>
There is other option to set the property in the main @SpringBootApplication class:
@SpringBootApplication public class ChangeApplicationPort < public static void main(String[] args) < SpringApplication app = new SpringApplication(ChangeApplicationPort.class); app.setDefaultProperties(Collections.singletonMap("server.port", "9001")); app.run(args); >>
The port defined inside the Custom Container always overrides the value defined inside application.properties .
If you are still using Spring Boot older version (not 2.x), you can use the EmbeddedServletContainerCustomizer option to set the port number.
3. Command Line Argument
We also have the option to set the port while starting our application. This is done by passing the argument through the command line.
java -Dserver.port=9001 -jar demo.jar java -jar demo.jar –server.port=9001
4. Environment Specific Port
Spring Profiles provides a powerful and easy way to control code and configuration based on the environment. Using Spring Profiles its possible to segregate parts of our application and make it only available in certain environments. We can use the same feature to change the default port based on the environment or profile.
To use profile specific configuration files, we need to the naming convention of application-.properties where profile defines the name of the intended profile. It will load profile from the same location as application.properties file. Let’s take an example, where we want to run our server on 9001 port on the dev environment while on the production we like to run our application on 9022 port.
To do this using configuration files, we will define 2 configuration files, namely application-production.properties and application-development.properties.
Set server.port property in application-development.properties file.
Set server.port property in application-production.properties file.
5. Configuration Evaluation Order
Spring Boot follows a certain order to load the configuration properties. While overriding the changes, please remember the sequence used by Spring Boot to load these configurations:
If you want Spring Boot to assigns random port for your application, set the port as 0 ( server.port=0 )
Summary
In this quick post, we cover the common ways to change the default port in Spring Boot. We learned how to do this using property file or by passing the port number through the command line argument. For most of the cases, using the application.properties or yml file is the most common option to change the default port in spring boot application, however, for many uses cases setting the port through command line or programmatically provides more flexibility. The source code for the article is available on the GitHub repository.