首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到'org.springframework.web.reactive.function.client.WebClient‘类型的Bean

找不到'org.springframework.web.reactive.function.client.WebClient‘类型的Bean
EN

Stack Overflow用户
提问于 2019-06-13 22:43:05
回答 2查看 7K关注 0票数 1

我正在使用这个手册https://golb.hplar.ch/2018/01/Sending-Web-push-messages-from-Spring-Boot-to-Browsers.html的例子来研究推送通知。我面临的问题是,当我运行应用程序时,我会得到以下错误

代码语言:javascript
复制
Parameter 1 of constructor in ru.stepanb.MetricPushingApplication.push.PushChuckJokeService required could not be found. 

Consider defining a bean of type 'org.springframework.web.reactive.function.client.WebClient' in your configuration. " 

代码语言:javascript
复制
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

@Service
public class PushChuckJokeService {

  private final FcmClient fcmClient;

  private final WebClient webClient;

  private int seq = 0;

  public PushChuckJokeService(FcmClient fcmClient, WebClient webClient) {
     this.fcmClient = fcmClient;
     this.webClient = webClient;
  }

  @Scheduled(fixedDelay = 30_000)
  public void sendChuckQuotes() {
    IcndbJoke joke = 
    this.webClient.get().uri("http://api.icndb.com/jokes/random")
    .retrieve().bodyToMono(IcndbJoke.class).block();
     try {
      sendPushMessage(joke);
     } 
    catch (InterruptedException | ExecutionException e) {
      //Application.logger.error("send chuck joke", e);
   }
  }

  void sendPushMessage(IcndbJoke joke) throws InterruptedException, 
    ExecutionException {
      Map<String, String> data = new HashMap<>();
      data.put("id", String.valueOf(joke.getValue().getId()));
      data.put("joke", joke.getValue().getJoke());
      data.put("seq", String.valueOf(this.seq++));
      data.put("ts", String.valueOf(System.currentTimeMillis()));

      System.out.println("Sending chuck joke...");
      this.fcmClient.send(data);
     }

  }

这是我的pom.xml文件

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.stepanb</groupId>
<artifactId>MetricPushingApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MetricPushingApplication</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.8.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.http-client</groupId>
                <artifactId>google-http-client-jackson</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.http-client</groupId>
                <artifactId>google-http-client-gson</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.api-client</groupId>
                <artifactId>google-api-client-gson</artifactId>
            </exclusion>
            <exclusion>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.auto.value</groupId>
                <artifactId>auto-value</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.threeten</groupId>
                <artifactId>threetenbp</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.cloud</groupId>
                <artifactId>google-cloud-firestore</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.apis</groupId>
                <artifactId>google-api-services-storage</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.api</groupId>
                <artifactId>gax</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.cloud</groupId>
                <artifactId>google-cloud-storage</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>3.0.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <debug>true</debug>
                <parameters>true</parameters>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                        <version>2.1.5.RELEASE</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>           
    </plugins>
</build>

和我的控制器

代码语言:javascript
复制
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.RestController;

    import reactor.core.publisher.Mono;

    @RestController
    @CrossOrigin
    public class RegistryController {

        private final FcmClient fcmClient;

        public RegistryController(FcmClient fcmClient) {
            this.fcmClient = fcmClient;
        }

        @PostMapping("/register")
        @ResponseStatus(HttpStatus.NO_CONTENT)
        public Mono<Void> register(@RequestBody Mono<String> token) {
            return token.doOnNext(t -> this.fcmClient.subscribe("chuck", t)).then();
        }

    }

你能给我解释一下当Spring应用程序是通过这行代码创建的时候,WebClient的创建是如何发生的吗?

代码语言:javascript
复制
SpringApplication.run(MetricPushingApplication.class, args);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-13 22:58:06

没有注册为bean的WebClient。

你可以像这样简单地使用它:

代码语言:javascript
复制
WebClient.create().get().uri("http://api.icndb.com/jokes/random")
                  .retrieve().bodyToMono(IcndbJoke.class).block();

或者您可以按照Spring Boot参考手册中的说明使用它

代码语言:javascript
复制
@Service
public class MyService {

    private final WebClient webClient;

    public MyService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("https://example.org").build();
    }

    public Mono<Details> someRestCall(String name) {
        return this.webClient.get().uri("/{name}/details", name)
                        .retrieve().bodyToMono(Details.class);
    }

}

请参阅:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.webclient

票数 3
EN

Stack Overflow用户

发布于 2021-07-14 12:32:42

您应该在Configuration类中配置bean。例如:

代码语言:javascript
复制
@Configuration
    class Configuration{
    @Bean
    WebClient webClient(WebClient.Builder builder) {
        return builder.build();
    }
    }

然后,您可以在其他类中使用webClient依赖项。例如:

代码语言:javascript
复制
class otherClass{
 private WebClient client;
 //Your use
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56583051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档