How to configure port for a Spring Boot application
NickName:Paul Verest Ask DateTime:2014-01-13T10:59:47

How to configure port for a Spring Boot application

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.

Copyright Notice:Content Author:「Paul Verest」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application

Answers
Luis Mauricio 2016-04-03T16:21:16

When you need a programatically way of doing it, you can set it during startup:\n\nSystem.getProperties().put( \"server.port\", 80 );\nSpringApplication.run(App.class, args);\n\n\nThis might help for things like environment dependent port. \nHave a nice day",


anandchaugule 2019-04-26T14:09:08

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.\n\n\n NOTE – you can use server.port=0 spring boot will find any unassigned http random port \n for us.\n\n\n1) application.properties\n\nserver.port=2020\n\n\n2) application.yml\n\nserver: \n port : 2020\n\n\n3) Change the server port programatically\n\n3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x\n\n@Component\npublic class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {\n\n @Override\n public void customize(TomcatServletWebServerFactory factory) {\n // customize the factory here\n factory.setPort(2020);\n }\n}\n\n\n3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x\n\n@Component\npublic class CustomizationBean implements EmbeddedServletContainerCustomizer {\n\n @Override\n public void customize(ConfigurableEmbeddedServletContainer container) {\n // customize here\n container.setPort(2020);\n }\n}\n\n\n4) By using command line option\n\n java -jar spring-boot-app.jar -Dserver.port=2020\n",


Paul Verest 2014-01-13T03:14:01

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\nserver.port=8090\n\nFor a random port use:\nserver.port=0\n\nSimilarly add application.yml in /src/main/resources/ with:\nserver:\n port: 8090\n",


VSharma 2019-10-24T09:20:02

if you are using gradle as the build tool, you can set the server port in your application.yml file as:\n\nserver:\n port: 8291\n\n\nIf you are using maven then the port can be set in your application.properties file as:\n\nserver.port: 8291\n",


Adrian Cosma 2015-12-15T09:11:46

Indeed, the easiest way is to set the server.port property.\n\nIf you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.\n\nThis editor provides autocomplete for all Spring Boot properties. If you write port and hit CTRL + SPACE, server.port will be the first option.",


anataliocs 2015-05-14T23:57:13

There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.\n\nModify application.properties\n\nFirst you can try the application.properties file in the /resources folder:\n\nserver.port = 8090\n\n\n\n\nModify a VM option\n\nThe 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:\n\nGo to Run -> Edit Configurations -> VM options\n\n-Dserver.port=8090\n\n\n\n\nAdditionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application",


Pau 2017-05-29T18:40:54

To extend other answers:\n\nThere is a section in the docs for testing which explains how to configure the port on integration tests:\n\n\n41.3 Testing Spring Boot applications\n41.3.3 Working with random ports\n\n\n\n\nAt integration tests, the port configuration is made using the annotation @SpringBootTest and the webEnvironment values.\n\n\n\nRandom port:\n\n@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)\n\n\nYou can inject the value using @LocalServerPort which is the same as @Value(\"${local.server.port}\").\n\n\nExample:\n\n\nRandom port test configuration:\n\n@RunWith(SpringRunner.class\n@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)\npublic class ExampleTest {\n ...\n @LocalServerPort //to inject port value\n int port;\n}\n\n\n\n\nDefined port:\n\n@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)\n\n\nIt takes the value from server.port if is defined.\n\n\nIf is defined using @TestPropertySource(properties = \"server.port=9192\"), it overrides other defined values.\nIf not, it takes the value from src/test/resources/application.properties (if exists). \nAnd finally, if it is not defined it starts with the default 8080.\n\n\nExample:\n\nDefined port test configuration:\n\n@RunWith(SpringRunner.class)\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)\n@TestPropertySource(properties = \"server.port=9192\")\npublic class DemoApplicationTests {\n\n @Test\n public void contextLoads() {\n }\n\n}\n",


Nafaz M N M 2020-05-14T09:46:25

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.\n server.port= 9876\n\nthen your application is running under the port 9876.",


tan9 2016-11-29T17:39:45

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:\n\n\nPass property through command line argument as application argument\n\njava -jar <path/to/my/jar> --server.port=7788\n\nFrom property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+)\n\n\nDefine environment variable in U*IX shell:\n\nSPRING_APPLICATION_JSON='{\"server.port\":7788}' java -jar <path/to/my/jar>\n\nBy using Java system property:\n\njava -Dspring.application.json='{\"server.port\":7788}' -jar <path/to/my/jar>\n\nPass through command line argument:\n\njava -jar <path/to/my/jar> --spring.application.json='{\"server.port\":7788}'\n\n\nDefine JVM system property\n\njava -Dserver.port=7788 -jar <path/to/my/jar>\n\nDefine OS environment variable\n\n\nU*IX Shell\n\nSERVER_PORT=7788 java -jar <path/to/my/jar>\n\nWindows\n\nSET SERVER_PORT=7788\njava -jar <path/to/my/jar>\n\n\nPlace property in ./config/application.properties configuration file\n\nserver.port=7788\n\n\nand run:\n\n java -jar <path/to/my/jar>\n\nPlace property in ./config/application.yaml\n\nserver:\n port: 7788\n\n\nand run:\n\n java -jar <path/to/my/jar>\n\nPlace property in ./application.properties\n\nserver.port=7788\n\n\nand run:\n\n java -jar <path/to/my/jar>\n\nPlace property in ./application.yaml\n\nserver:\n port: 7788\n\n\nand run:\n\n java -jar <path/to/my/jar>\n\n\n\n\n\nYou can combine above methods all together, and the former configuration in the list take precedence over the latter one.\n\nFor example:\n\nSERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788\n\n\nThe server will start and listen on port 7788.\n\nThis 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:\n\n\n Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.\n\n\n\n\nSERVER_NAME to server.name conversion was done by Relaxed Binding.",


nndru 2014-01-13T10:20:23

You can specify port by overriding EmbeddedServletContainerFactory bean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core \"Embedded Servlet Container Support\" paragraph and example there. Hope this helps.",


Sagar Mal Shankhala 2018-05-18T13:14:25

Hope this one help\n\n\napplication.properties=> \n\nserver.port=8090\n\napplication.yml=> \n\nserver\n port:8090\n",


makerj 2015-11-03T18:29:21

Also, you can configure the port programmatically.\nFor Spring Boot 2.x.x:\n@Configuration\npublic class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {\n public void customize(ConfigurableServletWebServerFactory factory){\n factory.setPort(8042);\n }\n}\n\nFor older versions:\n@Configuration\npublic class ServletConfig {\n @Bean\n public EmbeddedServletContainerCustomizer containerCustomizer() {\n return (container -> {\n container.setPort(8012);\n });\n }\n}\n",


Amit Gujarathi 2017-04-24T07:57:08

In application.properties file present in resources: \n\nserver.port=8082\n",


Ulises 2016-07-29T21:25:00

Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go. Just wanted to mention that you could also expose an environment property:\n\nSERVER_PORT=8080\n\n\nSince spring boot is able to replace \".\" for \"_\" and lower to UPPER case for environment variables in recent versions.\nThis is specially useful in containers where all you gotta do is define that environment variable without adding/editing application.properties or passing system properties through command line (i.e -Dserver.port=$PORT)",


itwarilal 2016-04-15T23:00:28

If you would like to run it locally, use this -\n\nmvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'\n\nAs of Spring Boot 2.0, here's the command that works (clues were here):\n\nmvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085\n",


Praneeth 2018-10-15T17:52:23

There are three ways to do it depending on the application configuration file you are using\n\na) If you are using application.properties file set\n\nserver.port = 8090\n\nb) If you are using application.yml file set server port property in YAML format as given below\n\nserver:\n port: 8090\n\n\nc) You can also Set the property as the System property in the main method\n\nSystem.setProperty(\"server.port\",\"8090\");\n",


gkarthiks 2016-02-02T15:45:24

You can add the port in below methods.\n\n\nRun -> Configurations section\nIn application.xml add server.port=XXXX\n",


ayurchuk 2016-03-17T16:58:47

You can set port in java code:\n\nHashMap<String, Object> props = new HashMap<>();\nprops.put(\"server.port\", 9999);\n\nnew SpringApplicationBuilder()\n .sources(SampleController.class) \n .properties(props)\n .run(args);\n\n\nOr in application.yml:\n\nserver:\n port: 9999\n\n\nOr in application.properties:\n\nserver.port=9999\n\n\nOr as a command line parameter:\n\n-Dserver.port=9999\n",


Deepesh kumar Gupta 2018-01-26T04:39:17

There are many other stuffs you can alter in server configuration by changing application.properties.\nLike session time out, address and port etc. Refer below post\n\nref: http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html\n\nI used few of them as below. \n\nserver.session.timeout=1\nserver.port = 3029\nserver.address= deepesh\n",


Shubham Khurana 2017-07-02T07:07:57

Just have a application.properties in src/main/resources of the project and give there \n\nserver.port=****\n\n\nwhere **** refers to the port number.",


gatolgaj 2014-12-03T19:30:42

In case you are using application.yml add the Following lines to it\n\nserver:\n port: 9000\n\n\nand of course 0 for random port. ",


Sridhar Battala 2016-06-17T09:57:41

Add this in your application.properties file\n\nserver.port= 8080\n",


Buddhika Lakshan 2017-08-28T10:01:59

1.1 Update via a properties file.\n\n/src/main/resources/application.properties\n\nserver.port=8888\n\nUpdate via a yaml file.\n\n server:\n\n port: 8888\n\n\nEmbeddedServletContainerCustomizer\n\n@Component\npublic class CustomContainer implements EmbeddedServletContainerCustomizer {\n\n @Override\n public void customize(ConfigurableEmbeddedServletContainer container) {\n\n container.setPort(8888);\n\n }\n\n}\n",


OlivierTerrien 2017-01-21T13:21:47

As explained in Spring documentation, there are several ways to do that:\n\nEither you set the port in the command line (for example 8888)\n\n-Dserver.port=8888 or --server.port=8888\n\nExample : java -jar -Dserver.port=8888 test.jar\n\nOr you set the port in the application.properties\n\nserver.port=${port:4588}\n\n\nor (in application.yml with yaml syntax)\n\nserver:\n port: ${port:4588}\n\n\nIf the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account. If not, then the port will be 4588 by default.\n\nIf you want to enforce the port in properties file whatever the environment variable, you just have to write:\n\nserver.port=8888\n",


Rakesh 2015-09-29T06:01:19

\nAs everyone said, you can specify in application.properties \nserver.port = 9000 (could be any other value)\nIf you are using spring actuator in your project, by default it points to\n8080, and if you want to change it, then in application.properties mention \nmanagement.port = 9001 (could be any other value)\n",


Ashwini 2018-10-15T12:52:26

Providing the port number in application.properties file will resolve the issue\n\n server.port = 8080\n\n\n\"port depends on your choice, where you want to host the application\"",


Chandramouli 2016-10-18T18:22:30

Include below property in application.properties\n\nserver.port=8080\n",


ZhaoGang 2018-12-20T01:28:21

In the application.properties file, add this line:\n\nserver.port = 65535\n\nwhere to place that fie:\n\n\n 24.3 Application Property Files\n \n SpringApplication loads properties from application.properties files\n in the following locations and adds them to the Spring Environment:\n\nA /config subdirectory of the current directory\nThe current directory\nA classpath /config package\nThe classpath root\n\n \n The list is ordered by precedence (properties defined in locations\n higher in the list override those defined in lower locations).\n\n\nIn my case I put it in the directory where the jar file stands.\n\nFrom: \n\nhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files",


Abhijeet 2016-05-13T22:54:24

You can set that in application.properties under /src/main/resources/\n\nserver.port = 8090\n",


More about “How to configure port for a Spring Boot application” related questions

How to configure port for a Spring Boot application

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.

Show Detail

How to configure port for a Spring Boot application

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.

Show Detail

How to configure port for a Spring Boot application

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.

Show Detail

How to find port number of a Spring Boot app?

How do I find the port number of my SpringBoot app so I can call it? I already tried setting -Dserver.port=8080 and--server.port=8080 in VM arguments and in src/main/resources/application.propertie...

Show Detail

How to change the port of a Spring Boot application using Gradle?

The simple question is: How can you change the Spring Boot application port with gradle? Here are already listed a lot of correct answers if you are not using gradle. So for none gradle issues, pl...

Show Detail

How to configure Tomcat shutdown port in Spring Boot?

Looking for way how to configure shutdown port in Spring boot app.

Show Detail

Configuring the AJP port on Jetty in a Spring boot application

I am trying to configure the AJP port for the Jetty server in my spring boot application. However, I've seen examples about the Tomcat AJP connector but not for Jetty. Can someone tell me how I sho...

Show Detail

Configure port of spring-boot application for deployment in weblogic

I want to deploy an application on a weblogic server with the port configured. Currently already deploy correctly with the defined context-path, but the value that I have defined in the port does not

Show Detail

Spring boot configure websocket on a different port than http

I have a spring boot application with websocket setup via sockJs. public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint("/gs-...

Show Detail

In Spring Boot, How to assign the exact same random port to another property in configure

I am running spring boot with grpc-spring-boot-starter as a grpc-server and also configured as an eureka client. Since I'm trying to launch more instances later, i want to assign a random port for...

Show Detail