How to configure port for a Spring Boot application
Solution 1
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
Solution 2
There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.
Modify application.properties
First you can try the application.properties file in the /resources folder:
server.port = 8090
Modify a VM option
The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:
Go to Run -> Edit Configurations -> VM options
-Dserver.port=8090
Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application
Solution 3
Since Spring Boot provides various configuration externalization mechanism (through various PropertySource
implementations and/or processors wired into Environment
object in order), you can set any property outside of your jar archive through following methods:
-
Pass property through command line argument as application argument
java -jar <path/to/my/jar> --server.port=7788
-
From property in
SPRING_APPLICATION_JSON
(Spring Boot 1.3.0+)-
Define environment variable in U*IX shell:
SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
-
By using Java system property:
java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>
-
Pass through command line argument:
java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
-
-
Define JVM system property
java -Dserver.port=7788 -jar <path/to/my/jar>
-
Define OS environment variable
-
U*IX Shell
SERVER_PORT=7788 java -jar <path/to/my/jar>
-
Windows
SET SERVER_PORT=7788 java -jar <path/to/my/jar>
-
-
Place property in
./config/application.properties
configuration fileserver.port=7788
and run:
java -jar <path/to/my/jar>
-
Place property in
./config/application.yaml
server: port: 7788
and run:
java -jar <path/to/my/jar>
-
Place property in
./application.properties
server.port=7788
and run:
java -jar <path/to/my/jar>
-
Place property in
./application.yaml
server: port: 7788
and run:
java -jar <path/to/my/jar>
You can combine above methods all together, and the former configuration in the list take precedence over the latter one.
For example:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
The server will start and listen on port 7788.
This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment. And it is the design philosophy of Spring Boot:
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
SERVER_NAME
to server.name
conversion was done by Relaxed Binding.
Solution 4
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);
});
}
}
Solution 5
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 (clues were here):
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
Paul Verest
Java Developer, Architect, Team Leader; Agile Coach, Tech Manager Authoring Nodeclipse, Anide.js, Enide Organizing http://szjug.github.io/ Building up JVMs, Spring.io and Node.js User Groups (Java, Groovy, Scala, Android, JavaScript)
Updated on July 08, 2022Comments
-
Paul Verest almost 2 years
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.
-
Betlista about 6 yearsIf someone interested, here is shown how to have multiple ports - stackoverflow.com/questions/36357135/…
-
Keaz almost 6 yearsif you use "yml" file for configuration then you can use this server: port: 8081 Also annotate you main class as "@SpringBootApplication" and remove @ EnableAutoConfiguration
-
Lahiru Samishka over 4 yearsyour project [application.properties] for add the server.port=8080
-
Atif about 4 yearsset
server.port=8080
in application properties. this configuration is inServerProperties.class
class underorg.springframework.boot.autoconfigure.web
.
-
-
Erik Martino over 9 years
-
yathirigan about 9 yearsthis didn't seem to work. I used server.port in the application.yml and it worked
-
azizunsal almost 9 yearsWhen random port is used, port info can get with
@Value("${local.server.port}")
-
alpert over 8 yearsActually command line option is --server.port=8090 not -Dserver.port=8090. docs.spring.io/spring-boot/docs/current/reference/html/…
-
sargas over 8 yearsAs a compliment to this answer: According to the spring docs there are other paths you can put
application.properties
on. In my case that helped a lot. -
Soumya Kanti over 8 years-Dserver.port=XXXX did not work for me. I used OS environment variable mode:
$ SERVER_PORT=8090 java -jar <path/to/my/jar>
-
Xdg over 8 yearsThis is working and very useful, when you have port in your own config file and want to set it during runtime.
-
Martin Hansen over 8 yearsThis was helpful when i needed to deploy an application to a AWS Elastic Beanstalk service, to get the port from an environment variable.
-
Mike3355 about 8 yearsIt is also worth noting that once you so this it will only matter locally. Once you deploy this application on a server `server.port = 8080 will be ignored.
-
Priidu Neemre almost 8 yearsThis is super useful when all you want is a self-contained unit or integration test, +1.
-
JimHawkins almost 8 yearsWelcome to SO :-) please look at How to Answer
-
higuaro over 7 yearsVery useful when the env variable for port is already defined under a different name.
-
Lucky over 7 yearsIs'nt it the
@Configuration
instead of@Controller
? Please update if so. -
Lucky over 7 yearsOr Also if you are using Intellij IDEA the autocomplete works too. ;)
-
Lucky over 7 yearsDo you mean
application.yml
and what IDE are you using? Please be specific. -
rzymek over 7 years
java -jar <my.jar> --server.port=8090
works in spring 1.4.0 -
tan9 over 7 yearsBoth (1)
java -Dserver.port=XXXX -jar <path/to/my/jar>
and (2)java -jar <path/to/my/jar> --server.port=YYYY
works. The first command definesserver.port
system property, and the second command pass the property through the command line arguments (String... args
in themain
method). Moreover, if you run withjava -Dserver.port=XXXX -jar <path/to/my/jar> --server.port=YYYY
,YYYY
takes precedence overXXXX
, this is why Spring Boot Externalized Configuration is so charming. -
hd1 about 7 years
System.setProperty("server.port", 80);
is another way to achieve the same. -
kap about 7 yearsIt should be
@Configuration
, but if you import it using@Import
it works, not sure if it is working with auto configuration search. -
David Pham over 6 yearsMany ways to change port on Spring Boot tomcat javabycode.com/spring-framework-tutorial/spring-boot-tutorial/…
-
Yogesh Borkhade over 6 yearssrc/resources/application.properties server.port=8080 or
-
jorgen.ringen about 6 yearsYou can set the server.port property in many different ways. Look at the configuration-documentation for spring boot and find the solution that suits you: docs.spring.io/spring-boot/docs/current/reference/html/…
-
Betlista about 6 yearsWhy to add same asnwer one year later?!? and
server.port 8080
is wrong syntax for Java property file... -
PeMa about 6 yearsFor a beginner: I tried creating a file
PortConfiguration.java
in the package folder and pasted this code here. However, I'm geting an error from VS code: Configuration cannot be resolved to a type. Any suggestions? -
mapm about 6 yearsStarting from Spring Boot 2, you should use
spring-boot.run.jvmArguments
. -
old-monk about 6 yearsIn case you prefer maven, do this
mvn spring-boot:run -Dserver.port=8888
-
geek almost 6 yearsapplication.yml => server: port: 8090
-
jocull almost 6 yearsHere's a current link to the programmatic override section: docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…
-
Brent Bradburn over 5 years
SERVER_PORT=8081 mvn spring-boot:run
-
chethu about 5 yearsapplication.yaml can also be used instead of properties.
-
Milgo about 5 yearsUsing the HashMap will work only if no port is set in applications.properties or .yml.
-
serv-inc almost 5 yearsIn STS 4 it is at run -> run configurations -> main, then scroll down to Table with Parameter Name and Value
-
nikiforovpizza almost 5 yearsit's a dublicate answer
-
Sudip Bhandari almost 5 yearsI can't think of a scenario where you would want to have random port number
-
iQuestProgrammer over 4 yearsfor application.properties its
server.port = 8291
-
Luis Mauricio over 4 years@hd1, I added our answers to the main answer, check it out and modify as you se fit please
-
velocity over 3 years-Dserver.port did not work but --server.port worked. Also it should be passed to java -jar command and not mvn. So for example after building the projekt using mvn you execute java -jar artifact.jar --server.port=9099 to run the server on another to port
-
Heybat over 3 yearsinteresting, I didn't know that it is possible to do it programmatically. Thanks for info
-
Dirk Schumacher almost 3 yearsWhy are there dots behind the given port '8080.' and '0.' ?
-
SeverityOne almost 3 yearsWhat do Maven and Gradle have to do with whether you use a properties or YAML file? The build process (Maven) is completely disparate from the application framework (Spring Boot).
-
Javi Vazquez almost 3 yearsI agree A) and B) should be your options if you are not doing anything weird
-
user2081279 over 2 yearsI have a 2nd "application" class as a utility, and I wanted to only set a different port in that one. Therefore all the config file based approaches were not helpful for me. Close to giving up I found your programatic approach. Thank you!
-
Philip Rego over 2 yearsThis does not work
-
Philip Rego over 2 yearsThis doesn't work. What application.properties? Which properties can overwrite this one? How can I be sure Spring is picking it up?
-
Anuj Dhiman over 2 yearsThere is nice explanation of ways to change port of spring boot javavogue.com/2019/02/…