15

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?

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Rajat
  • 161
  • 1
  • 1
  • 5
  • 2
    You have two separate problems here, one is about the `@LocalServerPort` deprecation warning and the other about the SQL query that doesn't execute. Which one are you asking about? – g00glen00b Jul 14 '22 at 05:58
  • 2
    For the future if something is deprecated and you want to find the replacement, search for "spring deprecated", or replace spring with java if it is something from the standard JDK. So in this case *spring LocalServerPort deprecated*. It is not kept a secret, you just need to do a quick one minute search to figure it out. – Gimby Jul 14 '22 at 07:36
  • @g00glen00b second one i just solve local port issue but that sql exception still coming.i am using tomcat 10 and in my project i manually removed javax to jakarta for servlet because i am using servlet 5 which will not support javax package. – Rajat Jul 14 '22 at 12:17
  • @Rajat The problem is that now it's kinda too late to change your question to focus on your SQL issue (as that would invalidate the answers). I suggest creating a new question focusing on the SQL grammar issue. I edited that part out of your question. – g00glen00b Jul 14 '22 at 13:58
  • 1
    @Gimby - Spring is among the worst documented projects I have ever used. And your suggested search query has this Stack Overflow question as its top result. – ArtOfWarfare Nov 03 '22 at 20:45

4 Answers4

22

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.

Puneet Kundliya
  • 264
  • 1
  • 4
8

The org.springframework.boot.web.server.LocalServerPort is deprecated.

You can import org.springframework.boot.test.web.server.LocalServerPort

kenu8
  • 81
  • 1
  • 1
4

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 can find the current docs here

Julian Eckhardt
  • 119
  • 1
  • 5
1

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.