WebClient 无法解析

Tob*_*emi 4 java spring intellij-idea gradle

为了利用新的WebClientAPI,我已将其包含spring-webflux在我的 Intellij 项目中。

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-webflux'
//    compile group: 'org.springframework', name: 'spring-webflux', version: '5.2.7.RELEASE'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,WebClient 仍未解决:

C:\Users\tobia\Documents\spring-app\service\Service.java:25: error: cannot find symbol
        System.out.println(WebClient.Builder());
                           ^
  symbol:   variable WebClient
  location: class Service
Run Code Online (Sandbox Code Playgroud)

依赖关系本身似乎已经解决,就像webflux现在在我的“外部库”列表中一样:

在此输入图像描述


有人知道为什么WebClient仍未解决吗?


我已经尝试了所有 4 个依赖项删除,但没有一个起作用:

    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile group: 'org.springframework', name: 'spring-webflux', version: '5.2.7.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation group: 'org.springframework', name: 'spring-webflux', version: '5.2.7.RELEASE'
Run Code Online (Sandbox Code Playgroud)

Cra*_*der 7

build.gradle缺少此依赖项:

compile group: 'org.springframework', name: 'spring-webflux', version: '5.2.7.RELEASE'
Run Code Online (Sandbox Code Playgroud)

工作证明:

网络客户端

确保重新导入依赖项

示例代码WebClient应如下所示:

        WebClient client3 = WebClient
                .builder()
                .baseUrl("http://localhost:8080")
                .defaultCookie("cookieKey", "cookieValue")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
                .build();
Run Code Online (Sandbox Code Playgroud)

请注意,它是WebClient.builder()而不是WebClient.Builder(),看起来你的方法名称中有一个拼写错误,替换Builder()builder(),它应该可以工作。

WebClient.Builder是一个接口,因此此代码无效:

System.out.println(WebClient.Builder());
Run Code Online (Sandbox Code Playgroud)

这是您代码的语法问题,与Gradle、依赖项或 IntelliJ IDEA 无关。