简体   繁体   中英

@LocalServerPort missing from Spring Boot 2.0.0.RELEASE

I upgraded my app from spring boot 1.5.9.RELEASE to 2.0.0.RELEASE, and I can no longer import org.springframework.boot.context.embedded.LocalServerPort . I was using this to inject the port the server is running on during a test:

public class Task1Test {

    @LocalServerPort
    private int port;

The Spring release notes do not mention this removal and @LocalServerPort was not deprecated .

Is there an equivalent in Spring Boot 2.0 that I can use?

Edit : I'm pretty sure that the class is gone. I'm getting these compilation errors:

[ERROR] ... Task1Test.java:[12,49]package org.springframework.boot.context.embedded does not exist
[ERROR] ... Task1Test.java:[46,6] cannot find symbol
  symbol:   class LocalServerPort

It looks like it was moved to org.springframework.boot.web.server.LocalServerPort without notice. Hope that helps.

@LocalServerPort is now in org.springframework.boot.test.web.server.LocalServerPort

Update your imports in the test code.

Relevant link to the Spring boot docs here https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/web/server/LocalServerPort.html

the change is notified here https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/server/LocalServerPort.html

Note that if you are using the Spring Boot Getting Started guides, the guides are still using the old namespace and hence you will see a build error without an option to fix. You will need to update this manually.

It seems it's been moved to spring-boot-starter-web dependency as per this API documentation.

Try adding this maven dependency to see if that fixes it

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM