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
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",


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.",


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",


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",


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",


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