Home > Software design >  'org.springframework.boot.web.server.LocalServerPort' is deprecated
'org.springframework.boot.web.server.LocalServerPort' is deprecated

Time:07-14

In spring-Boot 2.7.1 @LocalServerPort is depricated. what can be used in place @LocalServerPort. Build is failing with below error.

Unexpected error occurred in scheduled task
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select NAME,VALUE from APPS_CONFIGURATION where name like '%ruby.e2e.%']; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "select NAME,[*]VALUE from APPS_CONFIGURATION where name like '%ruby.e2e.%'"; expected "*, INTERSECTS, NOT, EXISTS, UNIQUE, INTERSECTS"; SQL statement:
select NAME,VALUE from APPS_CONFIGURATION where name like '%ruby.e2e.%' [42001-210]

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/doManualAdjustment": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

CodePudding user response:

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.

CodePudding user response:

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.

  • Related