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:
- A /config subdirectory of the current directory.
- The current directory
- A classpath /config package
- 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.
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:
- A /config subdirectory of the current directory.
- The current directory
- A classpath /config package
- 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.
I encountered the same problem, in my case, I didn't pass the args to SpringApplication.
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class);
}
should be
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
Videos
Spring Boot properties like server.port will only take effect if you use the embedded Tomcat. That is, if you start your application by executing your main method with SpringApplication.run() in it or by creating an executable JAR and starting it with java -jar.
When you deploy your application as a WAR archive into a stand-alone Tomcat, you have to configure Tomcat in the traditional way by editing server.xml and possibly other configuration files.
server.port property is meant to be used only with an embedded application server. If you want to use a standalone one, then the configuration needs to be done on application server itself. In case of tomcat, if is specified in server.xml file.
If you want to run the application on AWS Elastic Beanstalk, when you are creating the environment you can specify that you want to have an webserver + tomcat.
This way you won't need to worry about ports. Amazon will handle it for you.
Import the below package in your spring boot 2.7.1. use @LocalServerPort for the below-mentioned package.
org.springframework.boot.test.web.server
You can read about it here in the 2.7.18's Deprecated LocalServer Java API doc.
Alternative to use: org.springframework.boot.test.web.server.LocalServer Java API Doc
Once go through you query again for the SQL error.
The org.springframework.boot.web.server.LocalServerPort is deprecated.
You can import org.springframework.boot.test.web.server.LocalServerPort
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
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.