Reputation: 64012
How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080
.
Upvotes: 1103
Views: 1206479
Reputation: 763
We need to use server.port=8081
to use the port
you can use different port for different property file like
For application-prod.properties
you can use server.port=8082
and like wise
Upvotes: 0
Reputation: 11
Just go to application.properties
file and give custom port name i.e., server.port=8081
Upvotes: 0
Reputation: 735
in the file application.properties add the following: server.port=8888 THE PORT NEEDED ALWAYS MENTIONED HERE
Upvotes: 1
Reputation: 94
Using application.properties:
Create or open the application.properties file in your Spring Boot project's src/main/resources directory if it doesn't already exist.
Add the following line to specify the desired port (e.g., 8080):
server.port=8080
Replace 8080 with the port number you want to use.
Save the application.properties file and Start the Application.
Upvotes: 0
Reputation: 4702
Here's a Kotlin approach for setting the port in code:
fun main(args: Array<String>) {
runApplication<TestApplication>(*args) {
setDefaultProperties(mapOf(
"server.port" to 8081
))
}
}
Upvotes: 0
Reputation: 1414
If you would like to run it locally, use this -
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'
As of Spring Boot 2.0, here's the command that works:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
clues were at:
Upvotes: 117
Reputation: 1715
Running by Gradle:
./gradlew bootRun
./gradlew bootRun --args='--server.port=8888'
application.properties
file named PORT
, run this: PORT=8888 ./gradlew bootRun
Running by Maven:
mvnw spring-boot:run
mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
application.properties
file named PORT
, run this: SERVER_PORT=9093 mvn spring-boot:run
Using java -jar
:
./gradlew clean build
. We will find the jar file inside: build/libs/
folder.mvn clean install
. We will find the jar file inside:target
folder.java -jar myApplication. jar
java -jar myApplication.jar --port=8888
java -jar -Dserver.port=8888 myApplication.jar
SERVER_PORT
in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar
Upvotes: 10
Reputation: 308
Many parameters including server port can be changed in multiple ways. However there is an order of precedence as outlined below:
@Component
public class CustomConfiguration implements WebServerFactoryCustomizer {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(9090);
}
}
Here we are setting server port as 9090 which is hard coded in the code. To avoid hardcoding, we can assign a value from environment using @Value annotation in a bean class and the use it here.
Second precedence is assigned to command line arguments like below:
java -jar target/spring-boot-0.0.1-SNAPSHOT.jar --server.port=8092
Here we are telling the server to start listening at 8092. Note if we use both the above approaches together, it will ignore command line argument as custom code is given the first precedence.
Third precedence is assigned to OS environment variable. If none of the above two approaches is taken up, Spring will take server port from environment property. In case of deployment on Kubernetes, a property set under env section in Deployment yaml will be used.
Fourth precedence is assigned to profile specific application.properties file.
Fifth precedence is assigned to value assigned in application.properties file (which by default Spring Boot tries to find src/main/resources/config, if not found, then tries to find under src/main/resources).
Most manageable and useful approach can be combination of first and third approach. You can use an environment property and use that custom code.
Sample code:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentCustomizer {
@Value("${server.port}")
private int serverPort;
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
public int getServerPort() {
return serverPort;
}
}
@Configuration
public class CustomConfiguration
{
@Autowired
EnvironmentCustomizer envCustomizer;
@Bean
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerPortCustomizer() {
return factory -> factory.setPort(envCustomizer.getServerPort());
}
}
Upvotes: 0
Reputation: 124
Open the application.properties file in your spring boot application. And add the below property in the properties file.
server.port = 1443
This will be working fine and you can set any port number as per your wish.
Upvotes: 5
Reputation: 900
Aside from adding the port on application.properties, you can also easily achieve multiple ports for different environments, by combining the property files approach with Spring profiles. Specifically, we can create a property file for each environment.
For example, we'll have an application-dev.properties file with this content:
server.port=8081
Then you can add another application-qa.properties file with a different port:
server.port=8082
Upvotes: 1
Reputation: 64012
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
Upvotes: 1615
Reputation: 847
Via
application.properties
server.port = 8082
(or any new port number)
via
application.yml
server
port: 8082
Upvotes: 4
Reputation: 629
By Default Spring-web module provides an embedded tomcat server runs on port number 8080.
You can change it as follows -
A) If you are using gradle then use can set the property in your application.yml :
server:
port: 8042
B) If you are using maven then you can set the property in your application.properties :
server.port: 8042
C) When you have port in your own config file and want to set it during runtime.
By implementing WebServerFactoryCustomizer interface - Spring 2.x
@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(8042);
}
}
By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(8042);
}
}
Upvotes: 8
Reputation: 1080
Using mvn shell command line, spring-boot 2:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
Upvotes: 7
Reputation: 864
If you are use the spring command line interface (CLI) use the --
to separate commands from the spring
command arguments, to change the port:
spring run hello.groovy -- --server.port=9000
Upvotes: 0
Reputation: 41
Server port declare in two types
1.static type
server.port=8080. // your port number
Dynamic type
server.port=0. // randomly generate port number.
server.port=${PORT:0}
Upvotes: 3
Reputation: 2259
Also, you can configure the port programmatically.
For Spring Boot 2.x.x:
@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory){
factory.setPort(8042);
}
}
For older versions:
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}
Upvotes: 143
Reputation: 1698
By default, spring-web module provides an embedded tomcat server that is running under the port number 8080. If you need to change the port number of the application then go to application.properties
file and configure the port number by using server.port
property.
server.port= 9876
then your application is running under the port 9876.
Upvotes: 12
Reputation: 300
You can configure the port in the application.property file or application.yaml file which is in src/main/resources .
server.port=8080
Upvotes: 4
Reputation: 35
Configure the port details in Config file or application properties.
e.g.
port =8876
Upvotes: 0
Reputation: 24974
Just set the environment variable SERVER_PORT
.
(The examples works on Linux
)
Start via java -jar:
SERVER_PORT=9093 java -jar target/eric-sc-dummy.jar
Start via maven spring-boot plugin:
SERVER_PORT=9093 mvn spring-boot:run
Tips:
java -jar
or mvn
command, then you need to add export
to set env in a separate command, and split them via ;
, to make sure it's available to sub process.export SERVER_PORT=9093; export MAVEN_OPTS="-Xmx256m -Xms64m"; mvn spring-boot:run
Upvotes: 4
Reputation: 493
if you are using gradle as the build tool, you can set the server port in your application.yml file as:
server:
port: 8291
If you are using maven then the port can be set in your application.properties file as:
server.port: 8291
Upvotes: 21
Reputation: 8408
Programmatically, with spring boot 2.1.5:
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}
Upvotes: 2
Reputation: 2285
Apart from all the answers, I would like to point out that most IDE (IntelliJ with Spring plugin, STS) have a feature where it suggests all the configuration keys supported by SpringBoot. (i.e. all the opinionated configuration keywords)
Upvotes: 3
Reputation: 107
server.port = 0
for random port
server.port
= 8080 for custom 8080 port
Upvotes: 1
Reputation: 338
if your port number can be random you can use random function in your application.properties
server.port=${random.int(4)}
Upvotes: 1
Reputation: 1137
By default spring boot app start with embedded tomcat server start at default port 8080. spring provides you with following different customization you can choose one of them.
NOTE – you can use server.port=0 spring boot will find any unassigned http random port for us.
1) application.properties
server.port=2020
2) application.yml
server:
port : 2020
3) Change the server port programatically
3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x
@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(2020);
}
}
3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(2020);
}
}
4) By using command line option
java -jar spring-boot-app.jar -Dserver.port=2020
Upvotes: 14
Reputation: 38267
Similar to https://stackoverflow.com/a/36865796/1587329 and https://stackoverflow.com/a/40799750/1587329, a gradle one-liner would be
SERVER_PORT=9090 gradle bootRun
Upvotes: 2