Paul Verest
Paul Verest

Reputation: 64012

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.

Upvotes: 1103

Views: 1206479

Answers (30)

Rajat
Rajat

Reputation: 763

We need to use server.port=8081to 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

M Ali
M Ali

Reputation: 11

Just go to application.properties file and give custom port name i.e., server.port=8081

Upvotes: 0

Hilal Aissani
Hilal Aissani

Reputation: 735

in the file application.properties add the following: server.port=8888 THE PORT NEEDED ALWAYS MENTIONED HERE

Upvotes: 1

Amar kumar Nayak
Amar kumar Nayak

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.enter image description here

Upvotes: 0

vilpe89
vilpe89

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

itwarilal
itwarilal

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

Md. Shahariar Hossen
Md. Shahariar Hossen

Reputation: 1715

Running by Gradle:

  • Run in default port(8080): ./gradlew bootRun
  • Run in provided port(8888): ./gradlew bootRun --args='--server.port=8888'
  • If we have any variable in the application.properties file named PORT, run this: PORT=8888 ./gradlew bootRun

Running by Maven:

  • Run in default port(8080): mvnw spring-boot:run
  • Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
  • Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
  • Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
  • If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run

Using java -jar:

  • Create the .jar file:
    • For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder.
    • For Maven: mvn clean install. We will find the jar file inside:target folder.
  • Run in default port(8080): java -jar myApplication. jar
  • Run in provided port(8888): java -jar myApplication.jar --port=8888
  • Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar
  • Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar

Upvotes: 10

Soumen Ghosh
Soumen Ghosh

Reputation: 308

Many parameters including server port can be changed in multiple ways. However there is an order of precedence as outlined below:

  1. First precedence is assigned to custom code like 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.

  1. 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.

  1. 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.

  2. Fourth precedence is assigned to profile specific application.properties file.

  3. 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

Santil
Santil

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

Joel Wembo
Joel Wembo

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

Paul Verest
Paul Verest

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

Akash Verma
Akash Verma

Reputation: 847

Via

application.properties

server.port = 8082 (or any new port number)

via

application.yml

server
  port: 8082

Upvotes: 4

Ishan Garg
Ishan Garg

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

Ady Junior
Ady Junior

Reputation: 1080

Using mvn shell command line, spring-boot 2:

mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'

Upvotes: 7

Maske
Maske

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

spring-boot cli

Upvotes: 0

BRAIEK AYEMN
BRAIEK AYEMN

Reputation: 87

put this code in u applicatop.properties file
enter image description here

Upvotes: -1

Pandiyan MCA
Pandiyan MCA

Reputation: 41

Server port declare in two types

1.static type

   server.port=8080. // your port number
  1. Dynamic type

     server.port=0.      // randomly generate port number. 
     server.port=${PORT:0}
    

Upvotes: 3

makerj
makerj

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

Nafaz M N M
Nafaz M N M

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

Duleepa Wickramasinghe
Duleepa Wickramasinghe

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

enter image description here

Upvotes: 4

suveer
suveer

Reputation: 35

Configure the port details in Config file or application properties.

e.g.

port =8876

Upvotes: 0

Eric
Eric

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:

  • If you add other sub commands before the 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.
    e.g:
    export SERVER_PORT=9093; export MAVEN_OPTS="-Xmx256m -Xms64m"; mvn spring-boot:run

Upvotes: 4

VSharma
VSharma

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

Niel de Wet
Niel de Wet

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

Sudip Bhandari
Sudip Bhandari

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)

enter image description here

Spring Plugin for Intellij

Upvotes: 3

Jijo
Jijo

Reputation: 107

server.port = 0 for random port

server.port = 8080 for custom 8080 port

Upvotes: 1

Devilluminati
Devilluminati

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

Joyson Rego
Joyson Rego

Reputation: 1078

In Application properties just add 1 line

server.port = 8090

Upvotes: 2

anandchaugule
anandchaugule

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

serv-inc
serv-inc

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

Related Questions