Since Spring boot 2.7.1, @LocalServerPort
(in the package org.springframework.boot.web.server.LocalServerPort
) is deprecated.
What can I use in stead of this annotation?
Since Spring boot 2.7.1, @LocalServerPort
(in the package org.springframework.boot.web.server.LocalServerPort
) is deprecated.
What can I use in stead of this annotation?
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 link
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
For everybody looking for a solution: Simply replace your imports of
import org.springframework.boot.web.server.LocalServerPort;
by
import org.springframework.boot.test.web.server.LocalServerPort;
You may try using @Value("${server.port}")
to get the port. One thing to note here is since Spring Boot release 2.7.0, @LocalServerPort is moved to the test jar because the Spring Boot team only intended that they be used for tests. However, what Puneet suggests will also work provided that you have the below dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
You can also use an event listener to grab the port once the web server has started. Depending on what you're trying to do this might work, but be aware that they even fires after beans have been created.
@EventListener
void onWebInit(WebServerInitializedEvent event) {
int port = event.getWebServer().getPort();
}
The simplest approach here is to use either @Value("${server.port}")
or what Puneet suggests. Use the import from the test jar. And having the above-mentioned dependency in your pom.xml is vital for this to work.
You can checkout the github issue related to this migration.