Is the application.properties located at the right location? Description from Spring.io:

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

Use java -jar -Dserver.port=8090 jarfilename.jar to set the port from command line.

Hint, from Spring.io: If you want to use the short term -Dport=8090, you can use server.port=${port:8080} in your application property file.

Answer from iBiber on Stack Overflow
🌐
GitHub
github.com › spring-cloud › spring-cloud-config › issues › 1396
server.port property is not worked when combing Spring Cloud Config with other Spring projects. · Issue #1396 · spring-cloud/spring-cloud-config
May 24, 2019 - Bug report As title . Following is its reproduce step: set server: port: 8202 in application.yml together with below dependencies , tomcat start at port 8082 org.springframework.cloud spring-cloud-starter-config 2.1.0.RELEASE
Published   May 24, 2019
Author   5teven-tian
🌐
Baeldung
baeldung.com › home › spring › spring boot › how to change the default port in spring boot
How to Change the Default Port in Spring Boot | Baeldung
July 25, 2025 - For Spring Boot 1.x, we can similarly implement the EmbeddedServletContainerCustomizer interface. When packaging and running our application as a jar, we can set the server.port argument with the java command: ... As a final note, let’s look at the order in which these approaches are evaluated by Spring Boot.
🌐
GitHub
github.com › spring-projects › spring-boot › issues › 9770
Server port not resolved properly · Issue #9770 · spring-projects/spring-boot
July 17, 2017 - Using value server.port=8081 in environment variables is not resolved properly. While you don't normally use dots in env variables, we use that format in SCDF with ProcessBuilder. This used to work with boot 1.5 but now breaks with 2.x snapshots. spring-attic/spring-cloud-deployer-local#62
Author   jvalkeal
🌐
Spring
docs.spring.io › spring-boot › docs › 2.0.5.RELEASE › reference › html › howto-embedded-web-servers.html
75. Embedded Web Servers
To switch off the HTTP endpoints completely but still create a WebApplicationContext, use server.port=-1. (Doing so is sometimes useful for testing.) For more details, see “Section 27.4.4, “Customizing Embedded Servlet Containers”” in the ‘Spring Boot features’ section, or the ServerProperties source code.
🌐
Spring
docs.spring.io › spring-boot › docs › 1.3.0.RELEASE › reference › html › howto-properties-and-configuration.html
69. Properties & configuration - Spring
server: port: 9000 --- spring: profiles: development server: port: 9001 --- spring: profiles: production server: port: 0 · In this example the default port is 9000, but if the Spring profile ‘development’ is active then the port is 9001, and if ‘production’ is active then it is 0. The YAML documents are merged in the order they are encountered (so later values override earlier ones). To do the same thing with properties files you can use application-${profile}.properties to specify profile-specific values. Spring Boot binds external properties from application.properties (or .yml) (and other places) into an application at runtime.
🌐
Spring
docs.spring.io › spring-boot › docs › 1.3.3.RELEASE › reference › html › howto-properties-and-configuration.html
69. Properties & configuration
server: port: 9000 --- spring: profiles: development server: port: 9001 --- spring: profiles: production server: port: 0 · In this example the default port is 9000, but if the Spring profile ‘development’ is active then the port is 9001, and if ‘production’ is active then it is 0. The YAML documents are merged in the order they are encountered (so later values override earlier ones). To do the same thing with properties files you can use application-${profile}.properties to specify profile-specific values. Spring Boot binds external properties from application.properties (or .yml) (and other places) into an application at runtime.
🌐
Stack Overflow
stackoverflow.com › questions › 21083170 › how-to-configure-port-for-a-spring-boot-application › 65795203
java - How to configure port for a Spring Boot application - Stack Overflow
set server.port=8080 in application properties. this configuration is in ServerProperties.class class under org.springframework.boot.autoconfigure.web.
Find elsewhere
🌐
GitHub
github.com › spring-projects › spring-boot › issues › 5077
${local.server.port} not resolved in application.properties · Issue #5077 · spring-projects/spring-boot
February 3, 2016 - I can set this in src/test/resources/config/application.properties to get a free random port: server.port=0 Also I can get the actual random port injected into my test case like this: @Value("${local.server.port}") private int port; Howe...
Author   hohwille
🌐
GitHub
github.com › spring-projects › spring-boot › issues › 14877
Can't inject local.server.port into a @Configuration so it can be wired into a @Component · Issue #14877 · spring-projects/spring-boot
October 17, 2018 - @Configuration class ApolloConfig { @Bean @Profile( "!test" ) static ApolloClient.Builder prodClient( @Value( "${phdb.endpoint}" ) String graphqlEndpoint ) { return ApolloClient.builder().serverUrl( graphqlEndpoint ); } @Bean @Profile( "test" ) static ApolloClient.Builder testClient(@Value( "${local.server.port}" ) int port ) { return ApolloClient.builder(); } } ... Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}"
Author   xenoterracide
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot › spring boot: change default port of embedded server
Spring Boot: Change Default Port of Embedded Server
May 27, 2024 - To scan for a free port (using OS natives to prevent clashes), use ‘server.port=0‘. Now Spring boot will find any unassigned HTTP port for us.
🌐
ConcretePage
concretepage.com › spring-boot › spring-boot-change-default-server-port
Spring Boot Change Default Server Port
We can change spring boot default settings in eclipse by configuring Environment variable in run configurations. Step-1: Right click on the class and go to Run As -> Run Configurations Step-2: Click on the Environment tab and server port as follows.
🌐
Java Development Journal
javadevjournal.com › home › spring boot › how to change the default port in spring boot
How to Change the default port in Spring Boot | Java Development Journal
March 3, 2021 - Changing the default port in Spring Boot using application.properties is the most common and flexible way. We have the option to programmatically configure your embedded servlet container. To do this, create a Spring bean which implements the WebServerFactoryCustomizer interface. @Component public class CustomizationPort implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9001); } }
🌐
Stack Abuse
stackabuse.com › how-to-change-port-for-spring-boot-applications
How to Change Port for Spring Boot Applications
January 19, 2022 - There are two types of properties files typically used in Spring Boot projects - application.properties and application.yml. The application.properties file follows a simple key-value format, where each line represents a new key. The application.yml file follows the YAML format. Both of these are very human-readable and straightforward and typically, when you start out with a skeleton project, the server.port is the only setting you'll have. ... Note: You can set the port to a random available port, by setting it to 0.
🌐
Spring
docs.spring.io › spring-boot › how-to › webserver.html
Embedded Web Servers :: Spring Boot
server: port: 8443 ssl: key-store: "classpath:keystore.jks" key-store-password: "secret" key-password: "another-secret" Using configuration such as the preceding example means the application no longer supports a plain HTTP connector at port 8080. Spring Boot does not support the configuration ...
🌐
ADevGuide
adevguide.com › home › blog › an easy step-by-step guide to changing server port in a spring boot application [4 ways]
An Easy Step-By-Step Guide to Changing Server Port in a Spring Boot Application [4 Ways] | ADevGuide
February 10, 2026 - Set the port via environment variable (works across all methods): # Linux/Mac export SERVER_PORT=9090 java -jar myapp.jar # Windows set SERVER_PORT=9090 java -jar myapp.jar # Docker docker run -e SERVER_PORT=9090 -p 9090:9090 myapp · Spring Boot automatically maps SERVER_PORT to server.port property.
Top answer
1 of 4
12

You have to provide a value for webEnvironment. In your case DEFINED_PORT like this

@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

For details see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

2 of 4
4

Adding another alternate solution which I had elsewhere.

I had configured

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

and

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

Thinking that was it, and still getting this error even when specifying web environment etc. My ${local.server.port} seemed to be always null.

After some time, I noticed that my Spring Boot startup message contained no notion of the port it was using, so apparently it really didn't listen to any port at all - which explained why it was null in the first place. Adding actual container implementation dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

Caused this to appear on my logs:

2019-02-26 18:45:47.231  INFO 12504 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 43132 (http/1.1) with context path '/'

after which local.server.port and @LocalServerPort would also work.